How do I get the size of a folder with the Exchange Web Services 2010 managed API?

I am trying to use the EWS 2010 managed API to get the total size of a user's mailbox. I did not find a web service method to get this data, so I decided that 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 don’t understand what it asks me to do, or this method just does not work with EWS 2010.

Hiding in the insight of the code, I was able to write what I thought was a method that would recursively traverse the folder structure and result in a 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; } 

(Assume that there are no more than 10,000 folders in this folder. The figure shows that the safe bid ...)

Unfortunately this does not work.

I initiate 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 reference to a null object.

For clarity, I also tried just getting the size of the root folder:

 Console.Write(root.ManagedFolderInformation.FolderSize.ToString()); 

Which threw the same NRE exception, so I know that this is not the case as soon as you get to a certain depth in the directory tree that ManagedFolderInformation does not exist.

Any ideas on how to get the total size of a user's mailbox? Am I barking the wrong tree?

+4
source share
2 answers

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

 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); } } 
+8
source

The first link is how you want. The message describes that folders are not considered “managed folders” by default, so you get NRE in the ManagedFolderInformation property for some folders.

The message is supposed to add an extended property to the query for folders. Here's the MSDN page on how to do this using the managed API.

I tried to find a good example, but did not come up with it. This should indicate the right direction. If I find anything, I will update my answer.

+1
source

Source: https://habr.com/ru/post/1305874/


All Articles