ExchangeService.FindFolders不返回任何文件夹 [英] ExchangeService.FindFolders not returning any folders

查看:144
本文介绍了ExchangeService.FindFolders不返回任何文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以循环访问邮箱中许多已定义的文件夹.每个文件夹都包含另一个名为完成"的文件夹.下面的代码找到此"Complete"文件夹并获取其FolderId.

I have a function which loops through a number of defined folders in a mailbox. Each of the folders contains another folder called "Complete". The below code finds this "Complete" folder and gets its FolderId.

运行时,代码运行良好,然后过一会儿FindFoldersResults findFolderProcessed = service.FindFolders(folder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Complete"), view);不返回任何文件夹.逐步执行代码,一切似乎都应该正常工作,但是findFolderProcessed.Folders为空.

When run, the code works fine then after a while FindFoldersResults findFolderProcessed = service.FindFolders(folder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Complete"), view); returns no folders. Stepping through the code, everything seems like it should work, but findFolderProcessed.Folders is empty.

为什么对许多文件夹都可以使用然后停止?

Why would it work for a number of folders then stop?

ServicePointManager.ServerCertificateValidationCallback =
    ((sender, certificate, chain, sslPolicyErrors) => true);

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new NetworkCredential("xxx", "xxx", "xxx");
service.AutodiscoverUrl("xxx@xxx.com");
service.Url = new Uri("https://xxx/ews/exchange.asmx");

FolderView view = new FolderView(int.MaxValue);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;

SearchFilter[] parameters = new SearchFilter[3];
parameters[0] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x1");
parameters[1] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x2");
parameters[2] = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "x3");

SearchFilter.SearchFilterCollection filterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, parameters);

FindFoldersResults findFolder = service.FindFolders(new FolderId(WellKnownFolderName.Inbox, new Mailbox("xxx@xxx.com")), filterCollection, view);

foreach (Folder folder in findFolder.Folders)
{
    //FindFoldersResults tempResults = service.FindFolders(folder.Id, view);
    FindFoldersResults findFolderProcessed = service.FindFolders(folder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Complete"), view);
    FolderId ProcessedFolderID = findFolderProcessed.Folders[0].Id;

    //Other Processing
}

示例文件夹结构

x1
 -> Complete
x2
 -> Complete
x3
 -> Complete
...
xn
 -> Complete

大约有50个文件夹,其结构完全相同.

There are around 50 folders, structured the exact same way.

推荐答案

因此,问题在于service.FindFolders返回的是重复的文件夹,而循环试图将文件夹处理两次.因此,它第一次正确处理了文件夹,但是第二次却导致了此问题.

So the issue was that service.FindFolders was returning duplicate folders and the loop was trying to process the folders twice. So it processed the folders correctly the first time around, but on the second go it was causing this issue.

我不知道为什么它会返回重复项,但是要修复它,我只是使用以下代码代替了foreach (Folder folder in findFolder.Folders)来对findFolder进行了重复数据删除:

I don't know why it would be returning duplicates, but to fix it I simply deduped findFolder by using the below code in place of foreach (Folder folder in findFolder.Folders):

var folderCollection = findFolder.Folders.GroupBy(x => x.DisplayName).Select(g => g.First());

foreach (Folder folder in folderCollection)

如果有人知道为什么在最初的service.FindFolders通话中会重复复制文件夹,请在下面发表评论.

If anyone knows why the folders would have been duplicated in the initial service.FindFolders call, feel free to comment below.

这篇关于ExchangeService.FindFolders不返回任何文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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