Rebex EWS

EWS, SMTP, MIME, S/MIME libraries for .NET

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

Folder operations

The sample code on this page assumes you have already connected and authenticated to an Exchange server.

Folder IDs 

A folder ID uniquely identifies a folder at the Exchange server. In Rebex EWS, folder IDs are represented by EwsFolderId class.

// create EWS client instance, connect, log in
var ews = new Rebex.Net.Ews();
ews.Connect(hostname);
ews.Login(username, password);

// find the 'Orders' subfolder in the root folder
EwsFolderInfo ordersFolder = ews.FindFolder(EwsFolderId.Root, "Orders");
EwsFolderId ordersId = ordersFolder.Id;

// get a list of messages in the 'Orders' folder
var ordersList = ews.GetMessageList(ordersId, EwsItemFields.Fast);
' create EWS client instance, connect, log in
Dim ews = New Rebex.Net.Ews()
ews.Connect(hostname)
ews.Login(username, password)

' find the 'Orders' subfolder in the root folder
Dim ordersFolder As EwsFolderInfo = ews.FindFolder(EwsFolderId.Root, "Orders")
Dim ordersId As EwsFolderId = ordersFolder.Id

' get a list of messages in the 'Orders' folder
Dim ordersList = ews.GetMessageList(ordersId, EwsItemFields.Fast)

Tip: To get a list of subfolders, use GetFolderList method. To get an ID of a specific subfolder, use the FindFolder method.

Tip: See Exchange IDs for more information.

Tip: See also Accessing shared mailboxes.

Well-known folder IDs 

Additionally, well-known folders such as 'Inbox', 'Sent Items' or the root folder are also accessible using a set of predefined folder IDs. These constant IDs are accessible through static properties of EwsFolderId class.

// get a list of messages in the 'Inbox' well-known folder
var messages = ews.GetMessageList(EwsFolderId.Inbox, EwsItemFields.Fast);

// convert 'Inbox' well-known ID to an ordinary folder ID
EwsFolderId inboxId = EwsFolderId.Inbox;
inboxId = ews.GetFolderId(inboxId);
' get a list of messages in the 'Inbox' well-known folder
Dim messages = ews.GetMessageList(EwsFolderId.Inbox, EwsItemFields.Fast)

' convert 'Inbox' well-known ID to an ordinary folder ID
Dim inboxId As EwsFolderId = EwsFolderId.Inbox
inboxId = ews.GetFolderId(inboxId)

Well-known folder IDs:

  • EwsFolderId.Inbox
  • EwsFolderId.Calendar
  • EwsFolderId.Contacts
  • EwsFolderId.DeletedItems
  • EwsFolderId.Drafts
  • EwsFolderId.JunkEmail
  • EwsFolderId.Outbox
  • EwsFolderId.PublicFolders
  • EwsFolderId.Root
  • EwsFolderId.SentItems
  • EwsFolderId.Tasks

Getting folder info 

To retrieve information about a specific folder, use GetFolderInfo method that returns an instance of EwsFolderInfo object:

// create EWS client instance, connect, log in
// ...

// get info about 'Inbox' folder
EwsFolderInfo folder = ews.GetFolderInfo(EwsFolderId.Inbox);

// print some values:
Console.WriteLine("Folder: {0}", folder.Name);
Console.WriteLine("Total items: {0}", folder.TotalItemCount);
' create EWS client instance, connect, log in
' ...

' get info about 'Inbox' folder
Dim folder As EwsFolderInfo = ews.GetFolderInfo(EwsFolderId.Inbox)

' print 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 EwsFolderInfo objects that represent subfolders.

Getting list of folders 

To get a list of subfolders, use the GetFolderList method:

// create EWS client instance, connect, log in
// ...

// get a list of folders at the root level
IList<EwsFolderInfo> rootFolders =
    ews.GetFolderList(EwsFolderId.Root);

// get a list of all folders
IList<EwsFolderInfo> allFolders =
    ews.GetFolderList(EwsFolderId.Root, EwsTraversalScope.Deep);
' create EWS client instance, connect, log in
' ...

' get a list of folders at the root level
Dim rootFolders As IList(Of EwsFolderInfo) =
    ews.GetFolderList(EwsFolderId.Root)

' get a list of all folders
Dim allFolders As IList(Of EwsFolderInfo) =
    ews.GetFolderList(EwsFolderId.Root, EwsTraversalScope.Deep)

Finding a folder with a specific name 

To find a custom folder that can't be represented by a well-known folder ID, use FindFolder method:

// create EWS client instance, connect, log in
// ...

// find folder 'Orders' under 'Root'
EwsFolderInfo folder = ews.FindFolder(EwsFolderId.Root, "Orders");
' create EWS client instance, connect, log in
' ...

' find folder 'Orders' under 'Root'
Dim folder As EwsFolderInfo = ews.FindFolder(EwsFolderId.Root, "Orders")

Checking folder existence 

To determine whether a folder exists, use FolderExists method:

// create EWS client instance, connect, log in
// ...

// create folder 'Orders' only when it doesn't already exists
bool exists = ews.FolderExists(EwsFolderId.Root, "Orders");
if (!exists)
    ews.CreateFolder(EwsFolderId.Root, "Orders");
' create EWS client instance, connect, log in
' ...

' create folder 'Orders' only when it doesn't already exists
Dim exists As Boolean = ews.FolderExists(EwsFolderId.Root, "Orders")
If Not exists Then
    ews.CreateFolder(EwsFolderId.Root, "Orders")
End If

Creating and removing folders 

To create a new folder, use CreateFolder method. To remove an existing folder including its content , use DeleteFolder method.

// create EWS client instance, connect, log in
// ...

// create new folder 'Orders' under 'Inbox'
EwsFolderId folderId = ews.CreateFolder(EwsFolderId.Inbox, "Orders");

// delete desired folder
ews.DeleteFolder(folderId);
' create EWS client instance, connect, log in
' ...

' create new folder 'Orders' under 'Inbox'
Dim folderId As EwsFolderId = ews.CreateFolder(EwsFolderId.Inbox, "Orders")

' delete desired folder
ews.DeleteFolder(folderId)

Note: Use DeleteMode property to specify whether delete folder permanently or move it to Deleted Items folder.

Deleting folder content 

To delete content of a folder, use DeleteFolderContent method. You can choose whether to only delete items in the specified folder, or whether to delete subfolders as well.

// create EWS client instance, connect, log in
// ...

// delete items in the specified folder
ews.DeleteFolderContent(folderId, EwsDeleteScope.ItemsOnly);
' create EWS client instance, connect, log in
' ...

' delete items in the specified folder
ews.DeleteFolderContent(folderId, EwsDeleteScope.ItemsOnly)
Note: DeleteFolderContent method is available in Exchange 2010 SP1 and later.

Note: Use DeleteMode property to specify whether delete folder permanently or move it to Deleted Items folder.

Renaming folders 

To rename a folder, use Ews.RenameFolder method:

// create EWS client instance, connect, log in
// ...

// find a folder
EwsFolderInfo folder = ews.FindFolder(EwsFolderId.Root, "Orders");

// rename the folder to 'Orders 2015'
ews.RenameFolder(folder.Id, "Orders 2015");
' create EWS client instance, connect, log in
' ...

' find a folder
Dim folder As EwsFolderInfo = ews.FindFolder(EwsFolderId.Root, "Orders")

' rename the folder to 'Orders 2015'
ews.RenameFolder(folder.Id, "Orders 2015")

Copying and moving folders 

To move a subfolder to another parent folder, use Ews.MoveFolder method. To copy a subfolder, use Ews.CopyFolder method.

// create EWS client instance, connect, log in
// ...

// get a folder
EwsFolderInfo folder = ews.FindFolder(EwsFolderId.Root, "Orders");

// move or copy the folder to the 'Inbox' folder
if (move)
    ews.MoveFolder(folder.Id, EwsFolderId.Inbox);
else
    ews.CopyFolder(folder.Id, EwsFolderId.Inbox);
' create EWS client instance, connect, log in
' ...

' get a folder
Dim folder As EwsFolderInfo = ews.FindFolder(EwsFolderId.Root, "Orders")

' move or copy the folder to the 'Inbox' folder
If move Then
    ews.MoveFolder(folder.Id, EwsFolderId.Inbox)
Else
    ews.CopyFolder(folder.Id, EwsFolderId.Inbox)
End If

Tip: Both CopyFolder and MoveFolder methods return the ID of the new folder.

Back to feature list...