Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get all ITEMS from Folders and Sub-folders of PublicFolders Using EWS Managed API

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???"

like image 448
user1891567 Avatar asked Dec 14 '12 11:12

user1891567


People also ask

What is EWS Managed API?

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.

What is IPF note?

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.

What is folder bind?

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.


2 Answers

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.

like image 152
ono2012 Avatar answered Oct 11 '22 18:10

ono2012


You Need to perform a recursive traversal of the public Folder tree. You cannot perform a deep traversal on public folders.

like image 40
Henning Krause Avatar answered Oct 11 '22 18:10

Henning Krause