Garethp / php-ews

PHP Exchange Web Services
BSD 3-Clause "New" or "Revised" License
112 stars 45 forks source link

Obtaining Mailbox and Folder Size #218

Closed l0ckm4 closed 3 years ago

l0ckm4 commented 3 years ago

Is there any method available to be able to obtain the size of a mailbox and/or the size of a folder in a mailbox?

On the technet forum someone has said it is possible using extended properties but i have no idea how to do this using this library.

Now, it does appear that you can get the actual folder size from EWS using extended property:

PropertyTag: 0x0e08

PropertyType: Integer

Code Snippet
<GetFolder xmlns=".../messages"
            xmlns:t=".../types">
   <FolderShape>
     <t:BaseShape>AllProperties</t:BaseShape>
     <t:AdditionalProperties>
        <t:ExtendedFieldURI PropertyTag="0x0e08" PropertyType="Integer"/>
     </t:AdditionalProperties>
   </FolderShape>
   <FolderIds>
      <t:DistinguishedFolderId Id="inbox"/>
   </FolderIds>
</GetFolder>

Thanks in advance

Garethp commented 3 years ago

When you call getFolder in this library, you can pass along additional options which are just merged into the request. So you could try something like this:

$api->getFolder($id, [
    'AdditionalProperties' => [
        'ExtendedFieldURI' => ['PropertyTag' => '0x0e08', PropertyType => 'Integer']
    ]
]);

I'm not sure if that's entirely the right structure, but the idea is that the options keyed array gets transformed into the XML that you need to make the call, so by playing around with what you pass in there you can make the XML request look however you want to

l0ckm4 commented 3 years ago

Thanks Gareth

That put me in the right direction. I had to make a slight amendment to your suggestion. The actual code to get the folder with the folder size is :-

$options = array(
   'FolderShape' => array (
      'AdditionalProperties' => [
         'ExtendedFieldURI' => ['PropertyTag' => '0x0e08', 'PropertyType' => 'Integer']
      ]
   )
);

$api = MailAPI::withUsernameAndPassword($host,$username,$password,["version" => 'Exchange2010_SP1', 'impersonation' => 'someone@somewhere.com']);
$folder = $api->getFolderByDistinguishedId(\garethp\ews\API\Type\DistinguishedFolderIdNameType::INBOX, $options);
$inbox = array(
    'total emails' => $folder->getTotalCount(), 
    'unread emails' => $folder->getUnreadCount(), 
    'size of folder' => $folder->getExtendedProperty()->getValue()
);
print_r($inbox);

give me

Array
(
    [total emails] => 7440
    [unread emails] => 1
    [size of folder] => 1442608601
)

Which matches exactly with what I see in Outlook.

Cheers Mark