ExchangeService.FindFolders not returning any folders

丶灬走出姿态 提交于 2019-12-11 13:53:02

问题


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.

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
}

EDIT: Example Folder structure

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

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


回答1:


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.

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)

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



来源:https://stackoverflow.com/questions/36697994/exchangeservice-findfolders-not-returning-any-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!