Is it possible to search for all folders and sufolders in public folders by giving the path of the folders by using Exchange Web Service(EWS) Managed Api?
You can only search within folders, one level deep on EWS so for:
PublicFoldersRoot\subjectA\sectionB\partC\
I would search for "subjectA" folder, then once I have that FolderId then I would then search for "sectionB" folder and so on until I find what I need.
The method GetPublicFolderByPath
takes the path "subjectA\sectonB\partC\" and splits the path into an array of folder names, 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 method GetTopLevelFolder
gets the first folder, "sectionA" which is a child of the public folders root a.k.a. "WellKnownFolderName.PublicFoldersRoot".
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 takes a parent FolderId and searched all of the children folders for a match on the name provides and returns the child FolderId you have 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 the Microsoft.Exchange.WebServices managed API dll, similar idea for https://yourexchangeserver/ews/services.wsdl
. To get a folder from the path use create the ExchangeService object then write:GetPublicFolderByPath(service, "subjectA\sectionB\partC\")
Please up-vote if this helps you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With