HOW TO: List Top Level Folders in a Mailbox using EWS Managed API 1.0


Now that developers have started using Exchange Web Services Managed API, I thought it would be nice for me to post some samples to do simple things using the API. This sample shows:

1)How to list the Top Level folders in a mailbox.
2)Using Paging with the FolderView class.
3)Enabling logging of the Request and Response.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Xml;

namespace ListTopLevelFolders
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

// Enable Tracing
service.TraceListener = new TraceListener();
// Optional flags to indicate the requests and responses to trace.
service.TraceFlags = TraceFlags.EwsRequest | TraceFlags.EwsResponse;
service.TraceEnabled = true;

//Change the Credentials to suit you needs
service.Credentials = new WebCredentials("User", "Password", "domain");

//Use Autodiscover or Set the URL manually. Change the email address to match yours
service.AutodiscoverUrl("User@domain.com");

//Uncomment to list the Top level folders
ListTopLevelFolders(service,0);

Console.ReadLine();
}

static void ListTopLevelFolders(ExchangeService service,int moffset)
{
int mPageSize =5;
// Create a view with a page size of 5.
FolderView view = new FolderView(mPageSize,moffset,OffsetBasePoint.Beginning);

// Identify the properties to return in the results set.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.PropertySet.Add(FolderSchema.ChildFolderCount);

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

// Send the request to search the mailbox and get the results.
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, view);

// Process each item.
foreach (Folder myFolder in findFolderResults.Folders)
{
if (myFolder is SearchFolder)
{
Console.WriteLine("Search folder: " + (myFolder as SearchFolder).DisplayName);
}

else if (myFolder is ContactsFolder)
{
Console.WriteLine("Contacts folder: " + (myFolder as ContactsFolder).DisplayName);
}

else if (myFolder is TasksFolder)
{
Console.WriteLine("Tasks folder: " + (myFolder as TasksFolder).DisplayName);
}

else if (myFolder is CalendarFolder)
{
Console.WriteLine("Calendar folder: " + (myFolder as CalendarFolder).DisplayName);
}
else
{
// Handle a generic folder.
Console.WriteLine("Folder: " + myFolder.DisplayName);
}
}

// 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.
moffset = moffset + mPageSize;
ListTopLevelFolders(service, moffset);
}
else
{
Console.WriteLine("More Available: " + findFolderResults.MoreAvailable);
}
}
}

class TraceListener : ITraceListener
{
#region ITraceListener Members

public void Trace(string traceType, string traceMessage)
{
CreateXMLTextFile(traceType, traceMessage.ToString());
}

#endregion

private void CreateXMLTextFile(string fileName, string traceContent)
{
// Create a new XML file for the trace information.
try
{
// If the trace data is valid XML, create an XmlDocument object and save.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(traceContent);
xmlDoc.Save(fileName + ".xml");
}
catch
{
// If the trace data is not valid XML, save it as a text document.
System.IO.File.WriteAllText(fileName + ".txt", traceContent);
}
}
}
}

Enjoy!

Comments (0)

Skip to main content