How to get Exchange Online Folders

You can get all folders based in a criteria, this is useful because it can be queried by a SearchFilter. In this case I’m only get Folders that have at least 1 message because operator is SearchFilter.IsGreaterThan

 

SearchFilter searchFilter = new SearchFilter.IsGreaterThan
(FolderSchema.TotalCount, 0);
 
Unlike messages, in folders, you can use Deep Transversal’s, this means that you can get all Sub Folders on the fly.
 
 

view.Traversal = FolderTraversal.Deep;

 

As always you have to use an Object type of ExchangeService(), see the GetBinding() Method

 

Main Procedure

 

// Create a view with a page size of 10 or number you want.
FolderView view = new FolderView(10)

{
   
PropertySet = new PropertySet
     (BasePropertySet.IdOnly) { FolderSchema.DisplayName }
};

// Identify the properties to return in the results set.
// Return only folders that contain items.
SearchFilter searchFilter = new SearchFilter.IsGreaterThan

  (FolderSchema.TotalCount, 0);

// Unlike FindItem searches, folder searches can be deep traversals.
view.Traversal = FolderTraversal.Deep;

// Send the request to search the mailbox and get the results.
FindFoldersResults findFolderResults = service.FindFolders

  (WellKnownFolderName.Root, searchFilter, view);

// Process each item.
foreach (Folder myFolder in findFolderResults.Folders)

{
    // Go get myFolder.Id, MyFolder.DisplayName etc…
}

// Determine whether there are more folders to return.
if (findFolderResults.MoreAvailable)

{
   
// Make recursive calls with offsets set for the FolderView

    // to get the remaining folders in the result set for Paging.
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.