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

孤街醉人 提交于 2019-12-17 12:46:18

问题


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


回答1:


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    
}



回答2:


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




回答3:


public IEnumerable<Item> FindItemsInSubfolders(FolderId root, SearchFilter itemsFilter)
{
    foreach (Folder folder in service.FindFolders(root, new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep }))
        foreach(Item item in service.FindItems(folder.Id, itemsFilter, new ItemView(int.MaxValue)))
            yield return item;

    foreach (Item item in service.FindItems(root, itemsFilter, new ItemView(int.MaxValue)))
        yield return item;
}



回答4:


List<Folder> completeListOfFolderIds = new List<Folder>();
GetAllFolders(service, completeListOfFolderIds);
foreach (Folder folder in completeListOfFolderIds)
{
    List<Item> results = GetItems(service, folder);
}

public void GetAllFolders(ExchangeService service, List<Folder> completeListOfFolderIds)
{
    ExtendedPropertyDefinition isHiddenProp = new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean);
    FolderView folderView = new FolderView(int.MaxValue);
    folderView.PropertySet = new PropertySet(isHiddenProp,FolderSchema.DisplayName, FolderSchema.Id, FolderSchema.ChildFolderCount, FolderSchema.TotalCount, FolderSchema.ParentFolderId, FolderSchema.WellKnownFolderName);
    List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
    searchFilterCollection.Add(new SearchFilter.IsEqualTo(isHiddenProp, false));
    SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());
    Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
    // Indicate a Traversal value of Deep, so that all subfolders are retrieved.
    folderView.Traversal = FolderTraversal.Deep;
    rootfolder.Load();
    //FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderView);
    FindFoldersResults findFolderResults = rootfolder.FindFolders(searchFilter, folderView);
    foreach (Folder folder in findFolderResults)
    {
        completeListOfFolderIds.Add(folder);
        //List<Item> results = GetItems(service, folder);
        FindAllSubFolders(service, folder.Id, completeListOfFolderIds);                
    }
}

private void FindAllSubFolders(ExchangeService service, FolderId parentFolderId, List<Folder> completeListOfFolderIds)
{
    FolderView folderView = new FolderView(int.MaxValue);
    FindFoldersResults foundFolders = service.FindFolders(parentFolderId, folderView);
    completeListOfFolderIds.AddRange(foundFolders);

    // Now recurse
    foreach (Folder folder in foundFolders)
    {
        FindAllSubFolders(service, folder.Id, completeListOfFolderIds);
    }
}

private List<Item> GetItems(ExchangeService service ,Folder folder)
{
    if (service == null)
    {
        return null;
    }
    List<Item> items = new List<Item>();
    PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties);
    const Int32 pageSize = 50;
    ItemView itemView = new ItemView(pageSize);
    itemView.PropertySet = propertySet;
    FindItemsResults<Item> searchResults = null;
    try
    {
        do
        {                    
            searchResults = service.FindItems(folder.Id,itemView);
            items.AddRange(searchResults.Items);
            itemView.Offset += pageSize;
        } while (searchResults.MoreAvailable);
    }
    catch (Exception ex)
    {                
    }
    return items;
}


来源:https://stackoverflow.com/questions/13877629/how-to-get-all-items-from-folders-and-sub-folders-of-publicfolders-using-ews-man

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!