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
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
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.
I've put a download of full sample scripts for creating search folder for these two examples here
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
- $searchFolder.Save($sfRoot.Id);
- $PR_EXTENDED_FOLDER_FLAGS = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x36DA, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
- $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
- $psPropset.Add($PR_EXTENDED_FOLDER_FLAGS)
- $searchFolder.Load($psPropset)
- $exVal = $null
- if($SearchFolder.TryGetProperty($PR_EXTENDED_FOLDER_FLAGS,[ref]$exVal)){
- $newVal = "010402001000" + [System.BitConverter]::ToString($exVal).Replace("-","")
- $byteVal = HexStringToByteArray($newVal)
- $SearchFolder.SetExtendedProperty($PR_EXTENDED_FOLDER_FLAGS,$byteVal)
- $searchFolder.Update()
- }
- "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
- $PR_LAST_VERB_EXECUTED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x1081, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
- $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_LAST_VERB_EXECUTED,102)
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service);
- $rfRootFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
- $searchFolder.SearchParameters.RootFolderIds.Add($rfRootFolderId);
- $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;
- $searchFolder.SearchParameters.SearchFilter = $sfItemSearchFilter
- $searchFolder.DisplayName = $SearchFilterName
- $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)
- $sfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $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.
- [Guid]$psguid = "23239608-685D-4732-9C55-4C95CB4E8E33"
- $LatestMessageWordCount = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition($psguid,"LatestMessageWordCount", [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)
- $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan($LatestMessageWordCount,1000)
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- $SearchFolder = new-object Microsoft.Exchange.WebServices.Data.SearchFolder($service);
- $rfRootFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
- $searchFolder.SearchParameters.RootFolderIds.Add($rfRootFolderId);
- $searchFolder.SearchParameters.Traversal = [Microsoft.Exchange.WebServices.Data.SearchFolderTraversal]::Deep;
- $searchFolder.SearchParameters.SearchFilter = $sfItemSearchFilter
- $searchFolder.DisplayName = $SearchFilterName
- $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SearchFolders,$MailboxName)
- $sfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $searchFolder.Save($sfRoot.Id);
I've put a download of full sample scripts for creating search folder for these two examples here