Filter Messages With AQS From Exchange Mailbox
Advanced Query Syntax (AQS) is the query syntax used by Exchange as an alternative to searching filters for expressing search criteria. AQS is a more flexible way to perform searches and deliver search results for all commonly used fields on the items. AQS is also user-friendly, easy to understand and quickly to master. Using AQS is suitable for finding messages by attachments and recipients.
Creating a search query with AQS
You can create a search query with AQS by:
ExchangeAdvancedSyntaxQueryBuilder
, which represents the builder of search expression based on the Advanced Query Syntax (AQS). OrExchangeAdvancedSyntaxMailQuery
, which creates an AQS string directly based on the supported keywords.
Create a search query using query builder
To create a search query with ExchangeAdvancedSyntaxQueryBuilder you need to:
-
create an instance of IEWSClient using GetEWSClient method
-
create an instance of ExchangeAdvancedSyntaxQueryBuilder and set necessary properties to build a query.
-
call ListMessages or ListItems method and pass MailQuery instance, returned by GetQuery method, as one of its parameters.
The code sample below shows how the above steps can be accomplished:
using (var client = EWSClient.GetEWSClient(...))
{
var advancedBuilder = new ExchangeAdvancedSyntaxQueryBuilder();
advancedBuilder.From.Equals("Jim Martin");
advancedBuilder.Subject.Contains("report");
advancedBuilder.HasAttachment.Equals(true);
var messages = client.ListMessages(client.MailboxInfo.InboxUri, advancedBuilder.GetQuery());
}
Сreate a search query directly by using AQS:
To create a search query with ExchangeAdvancedSyntaxMailQuery you need to :
-
create an instance of IEWSClient using GetEWSClient method
-
create an instance of ExchangeAdvancedSyntaxMailQuery and pass an AQS string. See the syntax description.
-
call ListMessages or ListItems method and pass ExchangeAdvancedSyntaxMailQuery instance as one of its parameters.
The code sample below shows how the above steps can be accomplished:
using (var client = EWSClient.GetEWSClient(...))
{
ExchangeAdvancedSyntaxMailQuery query = new ExchangeAdvancedSyntaxMailQuery("subject:(product AND report)");
ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query);
}