I'm attempting to use EWS 2010 Managed API to get the total size of a user's mailbox. I haven't found a web service method to get this data, so I figured I would try to calculate it. I found one seemingly-applicable question on another site about finding mailbox sizes with EWS 2007, but either I'm not understanding what it's asking me to do, or that method just doesn't work with EWS 2010.
Noodling around in the code insight, I was able to write what I thought was a method that would traverse the folder structure recursively and result in a combined total for all folders inside the Inbox:
private int traverseChildFoldersForSize(Folder f)
{
int folderSizeSum = 0;
if (f.ChildFolderCount > 0)
{
foreach (Folder c in f.FindFolders(new FolderView(10000)))
{
folderSizeSum += traverseChildFoldersForSize(c);
}
}
folderSizeSum += (int)f.ManagedFolderInformation.FolderSize;
return folderSizeSum;
}
(Assumes there aren't more than 10,000 folders inside a given folder. Figure that's a safe bet...)
Unfortunately, this doesn't work.
I'm initiating the recursion with this code:
Folder root = Folder.Bind(svc, WellKnownFolderName.Inbox);
int totalSize = traverseChildFoldersForSize(root);
But a Null Reference Exception is thrown, essentially saying that [folder].ManagedFolderInformation
is a null object reference.
For clarity, I also attempted to just get the size of the root folder:
Console.Write(root.ManagedFolderInformation.FolderSize.ToString());
Which threw the same NRE exception, so I know that it's not just that once you get to a certain depth in the directory tree that ManagedFolderInformation doesn't exist.
Any ideas on how to get the total size of the user's mailbox? Am I barking up the wrong tree?
Using the EWS Managad APi, you can use this code to get the cumulative folder size of a mailbox:
The first link is the way you want to go. The post describes that the default folders are not considered "managed folders" which is why you are getting the NRE on the ManagedFolderInformation property for some folders.
What the post is suggesting is to add an Extended Property to the request for the folders. Here's the MSDN page on how to do that using the Managed API.
I tried to find a good example but didn't come up with one. This should point you in the right direction. If I find anything I'll update my answer.