Back to feature list...
Folder operations
On this page:
The sample code on this page assumes you have already connected and authenticated to Microsoft 365 (Exchange Online) server.
Folder IDs
A folder ID uniquely identifies a folder at the Exchange Online (Microsoft 365) server.
In Rebex Graph, folder IDs are represented by GraphFolderId class.
// create Graph client, connect, log in
// ...
// get list of folders in the mailbox
GraphFolderCollection folders = client.GetFolderList();
// display IDs and names of those folders
foreach (GraphFolderInfo folder in folders)
{
Console.WriteLine("{0}: {1}", folder.Id, folder.Name);
}
Tip: To get a list of subfolders, use GetFolderList()
method and specify the GraphFolderId argument.
Tip: Accessing shared mailboxes is possible as well.
Well-known folder IDs
Well-known folders such as 'Inbox', 'Drafts' or 'Sent Items' are accessible using a set predefined folder IDs. These constant IDs are accessible through static properties of GraphFolderId class:
GraphFolderId.Root- The root folder.GraphFolderId.Inbox- The "Inbox" folder.GraphFolderId.Drafts- The "Drafts" folder.GraphFolderId.SentItems- The "Sent Items" folder.GraphFolderId.Outbox- The "Outbox" folder.GraphFolderId.Archive- The "Archive" folder.GraphFolderId.DeletedItems- The "Deleted Items" folder.GraphFolderId.Deletions- The "Deletions" folder.GraphFolderId.JunkEmail- The "Junk E-mail" folder.GraphFolderId.Scheduled- The "Scheduled" folder (also known as "Snoozed").GraphFolderId.SearchFolders- The "Search Folders" folder (also known as "Finder").
Getting folder info
To retrieve information about a specific folder, use
GetFolderInfo() method
that returns an instance of GraphFolderInfo object:
// create Graph client instance, connect, log in
// ...
// get info about 'Inbox' folder
GraphFolderInfo folder = client.GetFolderInfo(GraphFolderId.Inbox);
// show some values
Console.WriteLine("Folder: {0}", folder.Name);
Console.WriteLine("Total items: {0}", folder.TotalItemCount);
Tip: GetFolderList is similar to GetFolderInfo, but it returns a list of
GraphFolderInfo objects that represent subfolders.
Getting list of subfolders
To get a list of subfolders, use GetFolderList() method:
// create Graph client instance, connect, log in // ... // find the 'Orders' folder (in the root folder) GraphFolderCollection folders = client.GetFolderList(); GraphFolderInfo folderOrders = folders.Where(s => s.Name == "Orders").First(); // list subfolders of the 'Orders' folder GraphFolderCollection subfolders = client.GetFolderList(folderOrders.Id);
Creating and removing folders
To create a new folder, use CreateFolder() method.
To remove an existing folder including its content, use DeleteFolder() method.
Note: Messages from the folder are soft-deleted (moved into 'Deletions' subfolder of 'Recoverable Items' folder).
// create Graph client instance, connect, log in // ... // create a folder in the root folder GraphFolderInfo folder = client.CreateFolder(GraphFolderId.Root, folderName); // create a subfolder in the new folder client.CreateFolder(folder.Id, subfolderName); // remove the folder along with its contents client.DeleteFolder(folder.Id);
Back to feature list...