Как получить все ITEMS из папок и подпапок PublicFolders, используя управляемый API EWS

Как получить все элементы из "общих папок" и его "подпапок" в Exchange Server2010 управляемый API uisng

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;
         } 
}

"Если мне это нравится, я не получаю элементы, присутствующие в подпапках. Папки в папках также не поддерживают запросы с глубоким обходом. Как я могу получить элементы из подпапок общих папок???"

Ответ 1

Чтобы все папки использовали следующий код:

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);
        }
    }

Чтобы получить все элементы:

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

Вам нужно выполнить рекурсивный обход общего дерева папок. Вы не можете выполнить глубокий обход общих папок.

Ответ 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;
}