This documentation is archived and is not being maintained.

Creating search folders 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 search folders. Search folders are created in the same way that regular folders are created. The difference between search folders and regular folders is that search folders have a property that defines the search filter that is used by the search.

Search folders can be created in any folder in an Exchange mailbox. However, for a search folder to appear in Microsoft Office Outlook, Outlook Web App, or Outlook Live, the folder must be created in the WellKnownFolderName.SearchFolders folder. If the search folder is created in a different location, it is still available and can be viewed by custom client applications.

To create a search folder

  1. Create a new search folder, as shown in the following example.

    // Create a new search folder.
    SearchFolder searchFolder = new SearchFolder(service);
  2. Define the search criteria. In the following example, the search criterion is the occurrence of the word "extended" in the subject line of the e-mail message.

    // Use the following search filter to get all mail in the Inbox with the word "extended" in the subject line.
    SearchFilter.ContainsSubstring searchCriteria =   new SearchFilter.ContainsSubstring(ItemSchema.Subject, "extended");
  3. Define the search parameters. In the following example, the search folder targets the Inbox. The display name of the search folder is "Extended".

    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Shallow;
    searchFolder.SearchParameters.SearchFilter = searchCriteria;
    searchFolder.DisplayName = "Extended";
  4. Save the search folder. After you save the folder, it is available to display the results of a search of an Exchange mailbox.

    Note Note

    If a folder of the same name already exists in an Exchange mailbox, a save operation will generate an error.


The following example shows how to create a search folder and save it to the WellKnownFolderName.SearchFolders folder. The search criterion that is defined for the search folder is e-mail messages in the Inbox that include the word "extended" in the subject line.

' Create a new search folder.
Dim searchFolder As New SearchFolder(service)

' Use the following search filter to get all mail in the Inbox with the word "extended" in the subject line.
Dim searchCriteria As New SearchFilter.ContainsSubstring(ItemSchema.Subject, "extended")

searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Shallow
searchFolder.SearchParameters.SearchFilter = searchCriteria
searchFolder.DisplayName = "Extended"
    Console.WriteLine(searchFolder.DisplayName & " added.")
Catch e As Exception
    Console.WriteLine("Error - " & e.Message)
End Try

The following example shows the XML request that is sent by the client to the server when the new search folder is saved to the Exchange database.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi=""
    <t:RequestServerVersion Version="Exchange2010" />
        <t:DistinguishedFolderId Id="searchfolders" />
            <t:Permissions />
          <t:SearchParameters Traversal="Shallow">
              <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="">
    <h:ServerVersionInfo MajorVersion="14" MinorVersion="0"
        MajorBuildNumber="639" MinorBuildNumber="20" Version="Exchange2010"
        xmlns:xsd="" />
  <s:Body xmlns:xsi=""
    <m:CreateFolderResponse xmlns:m=""
        <m:CreateFolderResponseMessage ResponseClass="Success">
              <t:FolderId Id="AAMkADBkOD" ChangeKey="CAAAABYAAA" />

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

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.