Utilizing the ExcludesBitmask SearchFilter Restriction in EWS

Utilizing the ExcludesBitmask SearchFilter Restriction in EWS Advanced properties are one of many tougher parts in relation to growing with Trade and bitwise properties are one of many tougher property varieties you might have to take care of. The commonest bitwise property in Trade you might entry/use is the PR_MessageFlags property https://msdn.microsoft.com/en-us/library/ee160304(v=exchg.80).aspx . In the event you take a look at this property on a Message that you simply acquired you may see one thing like

What the  ExcludesBitmask SearchFilter/Restriction in EWS means that you can do is create a search the place you may particularly exclude messages as a result of they’ve one in all these bitmask values set. Eg you may wish to exclude Draft messages out of your search or possibly you wish to exclude any messages that the place SentBy you within the Inbox. It’s also possible to use this Search Filter with the Not filter which Negates the restriction so if I created a Searchfilter to excluded all messages with the MSGFlag_HasAttached then negated that with the Not filter I’d then solely get the message the place the MSGFlag_HasAttached bit-wise is ready.

Utilizing this the EWS Managed API is fairly easy you first outline the property you wish to search on after which create a SearchFilter utilizing this property and the Bitwise Worth because the variable eg

ExtendedPropertyDefinition PR_MESSAGE_FLAGS = new ExtendedPropertyDefinition(0x0E07,MapiPropertyType.Integer);
SearchFilter ExcludeDraft = new SearchFilter.ExcludesBitmask(PR_MESSAGE_FLAGS, 8);

This creates a SearchFilter that excludes draft messages, If you wish to Negate that you need to use

SearchFilter JustDrafts = new SearchFilter.Not(ExcludeDraft);

Which might then create a filter that simply retrieved draft messages.

In PowerShell you may write a operate like the next

operate Question-MessageFlag  {
param(
[Parameter(Place=0, Necessary=$true)] [string]$MailboxName,
[Parameter(Place=1, Necessary=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Necessary=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Necessary=$false)] [string]$url,
[Parameter(Place=4, Necessary=$true)] [Int32]$Flag,
[Parameter(Place=5, Necessary=$true)] [string]$FolderPath,
[Parameter(Place=6, Necessary=$false)] [switch]$Negate,
[Parameter(Place=7, Necessary=$false)] [switch]$SummaryOnly
)
Start
{
if($url){
$service = Join-Trade -MailboxName $MailboxName -Credentials $Credentials -url $url
}
else{
$service = Join-Trade -MailboxName $MailboxName -Credentials $Credentials
}
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Trade.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$folderId = FolderIdFromPath -FolderPath $FolderPath -SmtpAddress $MailboxName
if($folderId -ne $null){
$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderId)
Write-Host ("Folder Name : " + $Folder.DisplayName)
$PR_MESSAGE_FLAGS = new-object Microsoft.Trade.WebServices.Knowledge.ExtendedPropertyDefinition(0x0E07,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
$sfItemSearchFilter = new-object Microsoft.Trade.WebServices.Knowledge.SearchFilter+ExcludesBitmask($PR_MESSAGE_FLAGS, $Flag)
if($Negate.IsPresent){
$sfItemSearchFilter = new-object Microsoft.Trade.WebServices.Knowledge.SearchFilter+Not($sfItemSearchFilter)
}
if(!$SummaryOnly.IsPresent){
$ivItemView = New-Object Microsoft.Trade.WebServices.Knowledge.ItemView(1000)
$fiItems = $null
do{
$fiItems = $service.FindItems($Folder.Id,$sfItemSearchFilter,$ivItemView)
#[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Merchandise in $fiItems.Gadgets){
Write-Host($Merchandise.DateTimeReceived.ToString() + " : " + $Merchandise.Topic)
}
$ivItemView.Offset += $fiItems.Gadgets.Depend
}whereas($fiItems.MoreAvailable -eq $true)
}
else
{
$ivItemView = New-Object Microsoft.Trade.WebServices.Knowledge.ItemView(1)
$fiItems = $service.FindItems($Folder.Id,$sfItemSearchFilter,$ivItemView)
Write-Host ("Total Items : " + $fiItems.TotalCount)
}
}
}
}

The above operate comes from the next script on Github https://github.com/gscales/Powershell-Scripts/blob/grasp/ExcludeQuery.ps1 demonstrates tips on how to both get Gadgets or a Abstract depend of these Gadgets primarily based on a bitwise flag exclusion or a negation of that exclusion. Once more some examples could also be so as

Instance 1 Seek for messages within the Inbox not despatched from you utilizing the mfFromMe bitwise flag

Question-MessageFlag -MailboxName consumer@area.com -FolderPath ‘Inbox’ -Flag 32 -SummaryOnly

Instance 2 Seek for messages within the Inbox  despatched from you utilizing the mfFromMe bitwise flag (that is utilizing a Negate filter)

Question-MessageFlag -MailboxName consumer@area.com -FolderPath ‘Inbox’ -Flag 32 -SummaryOnly -Negate

Instance Three Seek for messages with the Connect Flag set within the Inbox mfHasAttach

Question-MessageFlag -MailboxName consumer@area.com -FolderPath ‘Inbox’ -Flag 16 -SummaryOnly -Negate

Instance Four Seek for message with Learn Recipient flag mfNotifyRead and Negate that

Question-MessageFlag -MailboxName consumer@area.com -FolderPath ‘Inbox’ -Flag 256  -SummaryOnly -Negate

Leave a Reply