How To Access System Folders From EWS Managed API

The EWS Managed API provides a simple way to get to the Public Folders – you simply bind to the Microsoft.Exchange.WebServices.Data.WellKnownFolderName.PublicFoldersRoot, like so:

$pfRootName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot
$pfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $pfRootName)

But what if you want to access the Free Busy folder, or the Offline Address Book folders? How do you get to the System Folders? The WellKnownFolderName enumeration doesn’t have a value for the System Folders Root.

Fortunately, there’s a relatively simple trick you can use to get there. EWS gives you the option of binding to a folder by ID. The ID of the Public Folders Root will always end with the number 1, and the ID of the System Folders Root will always end with the number 2. So you can simply bind to the Public Folders Root, get the ID, swap out the number 1 with a number 2, and voila! You have the ID of the System Folders root. Here’s how you do it:

$pfRootName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot
$pfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $pfRootName)
$pfRootIdString = $pfRoot.Id.ToString()
$pfRootIdBytes = [System.Convert]::FromBase64String($pfRootIdString)
$pfRootIdBytes[$pfRootIdBytes.Length - 4] = [byte]0x02
$systemRootIdBase64String = [System.Convert]::ToBase64String($pfRootIdBytes)
$systemRootId = new-object Microsoft.Exchange.WebServices.Data.FolderId($systemRootIdBase64String)
$systemRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $systemRootId)

At that point you have the System Folders root in $systemRoot, and you can proceed from there. Hopefully this saves some of you some time. It was interesting trying to figure out how to do this!