This documentation is archived and is not being maintained.

Searching an Exchange mailbox by using the EWS Managed API 2.0

EWS Managed API

Learn how to use the EWS Managed API to perform a search on an Exchange mailbox.

Last modified: April 29, 2013

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

The QueryString element was introduced in Exchange Server 2010 to surface the preferred alternative for performing ad hoc Exchange mailbox searches. QueryString element searches use a content index to improve search performance. Unlike EWS search restrictions, when a QueryString element search is used, dynamic search folders are not created and temporarily persisted in the Exchange mailbox. The QueryString element takes a string argument that uses a format based on Advanced Query Syntax (AQS). For syntax information about the query string, see QueryString.

The following code example shows how to perform an Exchange search. The example shows a search performed on the Body property.

static void SearchByUsingFastSearch(ExchangeService service)
    // Return the first 10 items in this call.
    ItemView view = new ItemView(10);
    // Find all items where the body contains "move reports".
    string qstring = "Body:\"move reports\"";

    // Identify the item properties to return.
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly,
    // Send the request and get the results.          
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, qstring, view);

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.