You can only search inside folders at the same level in EWS so that:
PublicFoldersRoot \ subjectA \ sectionB \ PARTC \
I would search the βsubjectAβ folder, and then when I have this FolderId, then I would search the βsectionBβ folder and so on until I find what I need.
The GetPublicFolderByPath method takes the path "subjectA \ sectonB \ partC \" and splits the path into an array of folder names, and then finds each folder recursively.
public Folder GetPublicFolderByPath(ExchangeService service, String ewsFolderPath) { String[] folders = ewsFolderPath.Split('\'); Folder parentFolderId = null; Folder actualFolder = null; for (int i = 0; i < folders.Count(); i++) { if (0 == i) { parentFolderId = GetTopLevelFolder(service, folders[i]);// for first first loop public folder root is the parent actualFolder = parentFolderId; //in case folders[] is only one long } else { actualFolder = GetFolder(service, parentFolderId.Id, folders[i]); parentFolderId = actualFolder; } } return actualFolder; }
The GetTopLevelFolder method gets the first folder "sectionA", which is a child of the aka "WellKnownFolderName.PublicFoldersRoot" public folder root.
private Folder GetTopLevelFolder(ExchangeService service, String folderName) { FolderView folderView = new FolderView(int.MaxValue); FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView); foreach (Folder folder in findFolderResults) { if (folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase)) { return folder; } } throw new Exception("Top Level Folder not found: " + folderName); }
The GetFolder method accepts the parent FolderId and searches for all child folders to match the name and returns the child file FolderId that you requested.
private Folder GetFolder(ExchangeService service, FolderId ParentFolderId, String folderName) { FolderView folderView = new FolderView(int.MaxValue); FindFoldersResults findFolderResults = service.FindFolders(ParentFolderId, folderView); foreach (Folder folder in findFolderResults) { if (folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase)) { return folder; } } throw new Exception("Folder not found: " + folderName); }
Please note that I am using a DLL with a Microsoft.Exchange.WebServices managed API, a similar idea for https://yourexchangeserver/ews/services.wsdl . To get the folder from the path, create an ExchangeService object, and then write:
GetPublicFolderByPath(service, "subjectA\sectionB\partC\")
Please vote if this helps you :)