Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eeskildsen/e928472d11ba4723440e763ab5eb8b59 to your computer and use it in GitHub Desktop.
Save eeskildsen/e928472d11ba4723440e763ab5eb8b59 to your computer and use it in GitHub Desktop.
public static Folder FindFolderByDisplayName(ExchangeService service, string name)
{
var folderView = new FolderView(100);
folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
folderView.PropertySet.Add(FolderSchema.DisplayName);
folderView.Traversal = FolderTraversal.Deep;
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, folderView);
return findFolderResults
.Cast<Folder>()
.FirstOrDefault(folder => folder.DisplayName == name);
}
@JonasTaulien
Copy link

Hey, thanks for this!
Three additions for anyone who comes after me:

  1. Instead of filtering on the Server (and beeing limited to 100 folders), you can let EWS do the work for you by providing a search filter
  2. In my case, I was allowed to omit the PropertySet
  3. If you want to read out a specific mailbox of the ews-user, you can create a FolderId-Object containing that information

Result:

public static Folder FindFolderByDisplayName(ExchangeService service, string name)
{
    // 1. 
    SearchFilter searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, name);

    // 2.
    FolderView pagination = new FolderView(1)
    {
        Traversal = FolderTraversal.Deep
    };
    
    // 3.
    FolderId mailboxRootFolder = new FolderId(WellKnownFolderName.Root, "mailbox@example.com");

    FindFoldersResults results = service.FindFolders(mailboxRootFolder, searchFilter, pagination);

    return (results.TotalCount == 0)
        ? default(Folder)
        : results.Folders[0];
}

@andreasblueher
Copy link

Thank you to both of you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment