Skip to main content

Advanced Search Folder creation with EWS and Powershell

In the past I've posted some examples of creating SearchFolders in Exchange with EWS and Powershell in the following post,  in this post I want to cover some more advanced things you can do with EWS and Search Folders.

The first thing I want to cover is how you change the option on a Search folder from "Show number of unread items" which is the default when you create a search folder in EWS to "Show total number of items". eg




To change this option you need to set the PidTagExtendedFolderFlags property which is documented in http://msdn.microsoft.com/en-us/library/office/cc839880(v=office.15).aspx . This is a binary property and you set this particular option by changing bit 6-7 in this property. Because you also need the SearchFolderGuid in this property the easiest time is to set this is right after you create the folder. eg
  1. $searchFolder.Save($sfRoot.Id);  
  2. $PR_EXTENDED_FOLDER_FLAGS = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x36DA, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  3. $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  4. $psPropset.Add($PR_EXTENDED_FOLDER_FLAGS)  
  5. $searchFolder.Load($psPropset)  
  6. $exVal = $null  
  7. if($SearchFolder.TryGetProperty($PR_EXTENDED_FOLDER_FLAGS,[ref]$exVal)){  
  8.      $newVal = "010402001000" + [System.BitConverter]::ToString($exVal).Replace("-","")  
  9.      $byteVal = HexStringToByteArray($newVal)  
  10.      $SearchFolder.SetExtendedProperty($PR_EXTENDED_FOLDER_FLAGS,$byteVal)  
  11.      $searchFolder.Update()  
  12. }  
  13. "Folder Created"  

Some other interesting things you can do when using EWS to create search Folders is you can filter on some of the more advanced Item properties you can't access normally using Outlook. For example the PidTagLastVerbExecuted can be used to create a Search folder to find all email where the last action taken on the Message was to reply to the Sender. Eg this would create a SearchFolder with this type of filter
  1. $PR_LAST_VERB_EXECUTED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x1081, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)    
  2. $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_LAST_VERB_EXECUTED,102)   
  3. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  4. $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service);  
  5. $rfRootFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)     
  6. $searchFolder.SearchParameters.RootFolderIds.Add($rfRootFolderId);  
  7. $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;  
  8. $searchFolder.SearchParameters.SearchFilter = $sfItemSearchFilter  
  9. $searchFolder.DisplayName = $SearchFilterName  
  10. $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)     
  11. $sfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  12. $searchFolder.Save($sfRoot.Id);  

Another interesting Search Folder you can create on 2013/Office365 is using the LatestWordCount property which isn't a documented property but you can use it to produce a SearchFolder of all your wordy emails eg the following would create a SearchFolder of email where this property indicated the word count of the message was greater than 1000 words.
  1. [Guid]$psguid = "23239608-685D-4732-9C55-4C95CB4E8E33"  
  2. $LatestMessageWordCount = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition($psguid,"LatestMessageWordCount", [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)    
  3. $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan($LatestMessageWordCount,1000)   
  4. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  5. $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service);  
  6. $rfRootFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)     
  7. $searchFolder.SearchParameters.RootFolderIds.Add($rfRootFolderId);  
  8. $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;  
  9. $searchFolder.SearchParameters.SearchFilter = $sfItemSearchFilter  
  10. $searchFolder.DisplayName = $SearchFilterName  
  11. $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)     
  12. $sfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  13. $searchFolder.Save($sfRoot.Id);  

I've put a download of full sample scripts for creating search folder for these two examples here

Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.