How to retrieve all items from "public folders" and its "sub-folders" in exchange server2010 uisng managed API???
rootfolder = Folder.Bind(service,WellKnownFolderName.PublicFoldersRoot);
rootfolder.Load();
foreach (Folder folder in rootfolder.FindFolders(new FolderView(int.MaxValue)))
{
FindItemsResults<Item> findResults = folder.FindItems(iv);
foreach (Item item in findResults)
{
//get item info;
}
}
"If i do like this i am not getting items present in sub-folders of it..public folders does not support deep traversal queries too..How can i get items from sub-folders of public folders???"
The Exchange Web Services (EWS) Managed API provides a managed interface for developing .NET client applications that use EWS. By using the EWS Managed API, you can access almost all the information stored in an Office 365, Exchange Online, or Exchange Server mailbox.
IPF.Note. Email messages or folders. This is the generic folder class or type for the following EWS Managed API WellKnownFolderName folders and EWS DistinguishedFolderId folders: Root (IPM subtree) NonIpmSubtree.
Bind(ExchangeService, FolderId) Binds to an existing folder, whatever its actual type is, and loads its first class properties. Bind(ExchangeService, WellKnownFolderName) Binds to an existing folder, whatever its actual type is, and loads its first class properties.
To get all folders use the code below:
public void GetAllFolders(ExchangeService service, List<Folder> completeListOfFolderIds)
{
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView);
foreach (Folder folder in findFolderResults)
{
completeListOfFolderIds.Add(folder);
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
private void FindAllSubFolders(ExchangeService service, FolderId parentFolderId, List<Folder> completeListOfFolderIds)
{
//search for sub folders
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults foundFolders = service.FindFolders(parentFolderId, folderView);
// Add the list to the growing complete list
completeListOfFolderIds.AddRange(foundFolders);
// Now recurse
foreach (Folder folder in foundFolders)
{
FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
}
}
To get all items:
List<Folder> completeListOfFolderIds = new List<Folder>();
//Fills list with all public folders and subfolders
GetAllFolders(service, completeListOfFolderIds);
foreach(Folder folder in completeListOfFolderIds)
{
ItemView itemView = new ItemView(int.MaxValue);
FindItemsResults<Item> searchResults = service.FindItems(folder.Id, itemView);
//do something with item list
}
FYI FindFolders /FindItems only returns first 1000 items, so you'll have to alter this code to overcome that if you have massive structures.
You Need to perform a recursive traversal of the public Folder tree. You cannot perform a deep traversal on public folders.
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