Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get folder size with Exchange Web Services 2010 Managed API?

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?

like image 515
Adam Tuttle Avatar asked Apr 02 '10 19:04

Adam Tuttle


Video Answer


2 Answers

Using the EWS Managad APi, you can use this code to get the cumulative folder size of a mailbox:

internal class Program
{
    private static readonly ExtendedPropertyDefinition PidTagMessageSizeExtended = new ExtendedPropertyDefinition(0xe08,
                                                                                                                  MapiPropertyType
                                                                                                                    .Long);

    public static void Main(string[] args)
    {
        var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
                      {Credentials = new NetworkCredential("mail", "pw!")};

        service.AutodiscoverUrl("mail", url => true);

        var offset = 0;
        const int pagesize = 12;
        long size = 0;

        FindFoldersResults folders;
        do
        {
            folders = service.FindFolders(WellKnownFolderName.MsgFolderRoot,
                                          new FolderView(pagesize, offset, OffsetBasePoint.Beginning)
                                          {
                                            Traversal = FolderTraversal.Deep,
                                            PropertySet =
                                                new PropertySet(BasePropertySet.IdOnly, PidTagMessageSizeExtended,
                                                                FolderSchema.DisplayName)
                                          });
            foreach (var folder in folders)
            {
                long folderSize;
                if (folder.TryGetProperty(PidTagMessageSizeExtended, out folderSize))
                {
                    Console.Out.WriteLine("{0}: {1:00.00} MB", folder.DisplayName, folderSize/1048576);
                    size += folderSize;
                }
            }
            offset += pagesize;
        } while (folders.MoreAvailable);
        Console.Out.WriteLine("size = {0:0.00} MB", size/1048576);
    }
}
like image 181
Henning Krause Avatar answered Sep 28 '22 05:09

Henning Krause


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.

like image 45
Joe Doyle Avatar answered Sep 28 '22 06:09

Joe Doyle