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

后端 未结 4 977
小蘑菇
小蘑菇 2020-12-03 17:33

How to retrieve all items from \"public folders\" and its \"sub-folders\" in exchange server2010 uisng managed API???

rootfolder = Folder.Bind(service,WellKn         


        
4条回答
  •  执笔经年
    2020-12-03 18:12

    To get all folders use the code below:

    public void GetAllFolders(ExchangeService service, List 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 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 completeListOfFolderIds = new List();
    //Fills list with all public folders and subfolders
    GetAllFolders(service, completeListOfFolderIds);
    foreach(Folder folder in completeListOfFolderIds)
    {
    ItemView itemView = new ItemView(int.MaxValue);
    FindItemsResults 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.

提交回复
热议问题