使用EWS按路径进入Exchange文件夹 [英] Get to an Exchange folder by path using EWS

查看:111
本文介绍了使用EWS按路径进入Exchange文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用EWS从"Inbox \ test \ final" Exchange文件夹中检索项目.该文件夹由上面写的文字路径提供.我知道我可以将此字符串拆分为文件夹名称,然后递归搜索所需的文件夹,但是有没有更优化的方法可以将字符串路径转换为文件夹实例或文件夹ID?

我正在使用最新的EWS 2.0程序集.这些程序集是否提供任何帮助,还是让我难以进行手动递归?

解决方案

由于Exchange Server喜欢将所有内容与Folder.Id一起映射,因此查找所需路径的唯一方法是查看文件夹名称. /p>

您需要创建一个递归函数,以遍历文件夹集合中的所有文件夹,并跟踪其在电子邮件文件夹树中的移动路径.

需要另一个参数来跟踪您要查找的路径.

public static Folder GetPathFolder(ExchangeService service, FindFoldersResults results,
                                   string lookupPath, string currentPath)
{
    foreach (Folder folder in results)
    {
        string path = currentPath + @"\" + folder.DisplayName;
        if (folder.DisplayName == "Calendar")
        {
            continue;
        }

        Console.WriteLine(path);

        FolderView view = new FolderView(50);
        SearchFilter filter = new SearchFilter.IsEqualTo(FolderSchema.Id, folder.Id);
        FindFoldersResults folderResults = service.FindFolders(folder.Id, view);
        Folder result = GetPathFolder(service, folderResults, lookupPath, path);
        if (result != null)
        {
            return result;
        }

        string[] pathSplitForward = path.Split(new[] {  "/" }, StringSplitOptions.RemoveEmptyEntries);
        string[] pathSplitBack    = path.Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
        string[] lookupPathSplitForward = lookupPath.Split(new[] {  "/" }, StringSplitOptions.RemoveEmptyEntries);
        string[] lookupPathSplitBack    = lookupPath.Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);

        if (ArraysEqual(pathSplitForward, lookupPathSplitForward) || 
            ArraysEqual(pathSplitBack,    lookupPathSplitBack) || 
            ArraysEqual(pathSplitForward, lookupPathSplitBack) || 
            ArraysEqual(pathSplitBack,    lookupPathSplitForward))
        {
            return folder;
        }
    }
    return null;
}

"ArraysEqual":

public static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1, a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}

我进行了所有额外的数组检查,因为有时我的客户输入的路径带有正斜杠,反斜杠,以斜杠开头等.它们不精通技术,所以让我们确保程序每次都能正常工作!

浏览每个目录时,将所需路径与迭代路径进行比较.找到后,将其当前所在的Folder对象冒泡.您需要为该文件夹的ID创建搜索过滤器:

FindItemsResults<item> results = service.FindItems(foundFolder.Id, searchFilter, view);

浏览结果中的电子邮件!

foreach (Item item in results) 
{
    // do something with item (email)
}

I need to retrieve items from the 'Inbox\test\final' Exchange folder using EWS. The folder is provided by a literal path as written above. I know I can split this string into folder names and recursively search for the necessary folder, but is there a more optimal way that can translate a string path into a folder instance or folder ID?

I'm using the latest EWS 2.0 assemblies. Do these assemblies provide any help, or am I stuck with manual recursion?

解决方案

Since Exchange Server likes to map everything together with Folder.Id, the only way to find the path you're looking for is by looking at folder names.

You'll need to create a recursive function to go through all folders in a folder collection, and track the path as it moves through the tree of email folders.

Another parameter is needed to track the path that you're looking for.

public static Folder GetPathFolder(ExchangeService service, FindFoldersResults results,
                                   string lookupPath, string currentPath)
{
    foreach (Folder folder in results)
    {
        string path = currentPath + @"\" + folder.DisplayName;
        if (folder.DisplayName == "Calendar")
        {
            continue;
        }

        Console.WriteLine(path);

        FolderView view = new FolderView(50);
        SearchFilter filter = new SearchFilter.IsEqualTo(FolderSchema.Id, folder.Id);
        FindFoldersResults folderResults = service.FindFolders(folder.Id, view);
        Folder result = GetPathFolder(service, folderResults, lookupPath, path);
        if (result != null)
        {
            return result;
        }

        string[] pathSplitForward = path.Split(new[] {  "/" }, StringSplitOptions.RemoveEmptyEntries);
        string[] pathSplitBack    = path.Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
        string[] lookupPathSplitForward = lookupPath.Split(new[] {  "/" }, StringSplitOptions.RemoveEmptyEntries);
        string[] lookupPathSplitBack    = lookupPath.Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);

        if (ArraysEqual(pathSplitForward, lookupPathSplitForward) || 
            ArraysEqual(pathSplitBack,    lookupPathSplitBack) || 
            ArraysEqual(pathSplitForward, lookupPathSplitBack) || 
            ArraysEqual(pathSplitBack,    lookupPathSplitForward))
        {
            return folder;
        }
    }
    return null;
}

"ArraysEqual":

public static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1, a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}

I do all the extra array checking since sometimes my clients enter paths with forward slashes, back slashes, starting with a slash, etc. They're not tech savvy so let's make sure the program works every time!

As you go through each directory, compare the desired path to the iterated path. Once it's found, bubble up the Folder object that it's currently on. You'll need to create a search filter for that folder's id:

FindItemsResults<item> results = service.FindItems(foundFolder.Id, searchFilter, view);

Loop through the emails in results!

foreach (Item item in results) 
{
    // do something with item (email)
}

这篇关于使用EWS按路径进入Exchange文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆