Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Filtering on ContainsSubstring by using the EWS Managed API 2.0

EWS Managed API

Last modified: October 13, 2012

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.

You can use the Microsoft Exchange Web Services (EWS) Managed API to create a SearchFilter.ContainsSubstring search filter. The ContainsSubstring filter enables you to search an Exchange mailbox for items or folders that contain a substring inside a text property.

To use a ContainsSubstring search filter

  1. Define a view along with a page size. The following code identifies an ItemView with a page size of 10.

    ItemView view = new ItemView(10);

    The page size is required to create an ItemView. Other optional parameters for the ItemView constructor are the Offset and the OffsetBasePoint. The Offset and OffsetBasePoint are used for subsequent paged calls.

  2. Optionally, to enhance performance, identify the properties to return in the results set. The following example shows a property set that includes the item Id and Subject properties.

    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
  3. Create the search filter. The following example shows a search filter that is used to find items that contain the string "extended" in the item's Subject line.

    SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "extended");
  4. Send the request to search the mailbox and get the results. In this example, the search is limited to the mailbox user's Inbox.

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, filter, view);
  5. Do something with the FindItemsResult object.

The following example shows how to search the Exchange mailbox for any e-mail messages in the user's Inbox that contains the word "extended" in the Subject line. The example displays the total number of e-mail messages that satisfy the search filter.

// Obtain a collection of e-mail messages that satisfy a specified Search filter.
ItemView view = new ItemView(10);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

// Define the SearchFilter. 
// ContainsSubstring - Filter on mail with the word "extended" in the Subject line.
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "extended");

FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, filter, view);
Console.Write("ContainsSubstring: Total e-mail count with the word 'extended' in the subject: " + findResults.TotalCount);

This example assumes that the ExchangeService object named service is correctly configured to connect to the user’s Client Access server.

The following example shows the XML request that is sent by the client to the server when a ContainsSubstring search filter is used to search an Exchange mailbox.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <t:RequestServerVersion Version="Exchange2010" />
    <m:FindItem Traversal="Shallow">
          <t:FieldURI FieldURI="item:Subject" />
      <m:IndexedPageItemView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning" />
        <t:Contains ContainmentMode="Substring" ContainmentComparison="IgnoreCase">
          <t:FieldURI FieldURI="item:Subject" />
          <t:Constant Value="extended" />
        <t:DistinguishedFolderId Id="inbox" />

The following example shows the XML response that is returned by the server after it parses the request from the client. The ItemId and ChangeKey attributes have been shortened to preserve readability.

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <h:ServerVersionInfo MajorVersion="14" MinorVersion="0" MajorBuildNumber="499" MinorBuildNumber="0"
       Version="Exchange2010" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
       xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:FindItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
        <m:FindItemResponseMessage ResponseClass="Success">
          <m:RootFolder IndexedPagingOffset="4" TotalItemsInView="4" IncludesLastItemInRange="true">
                <t:ItemId Id="AAMkADBkODh"  ChangeKey="CQAAABYAAA" />
                <t:Subject>extended message</t:Subject>
                <t:ItemId Id="AAMkADBkODh="  ChangeKey="CQAAABYAAA" />
                <t:ItemId Id="AAMkADBkODh"  ChangeKey="CQAAABYAAA" />
                <t:Subject>Saved with extendedPropertyDefinition of two days</t:Subject>
                <t:ItemId Id="AAMkADBkODh"  ChangeKey="CQAAABYAAA" />
                <t:Subject>Extended property removed</t:Subject>

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.

© 2015 Microsoft