anothershrubery
anothershrubery

Reputation: 21003

ExchangeService.FindFolders not returning any folders

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("[email protected]");
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("[email protected]")), 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.

Upvotes: 2

Views: 2188

Answers (1)

anothershrubery
anothershrubery

Reputation: 21003

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.

Upvotes: 1

Related Questions