Partager via


Updating search folders by using the EWS Managed API 2.0

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 update search folders. Search folder updates can include changes to the search filter criteria, the target folders that are searched, and the search folder properties, including extended properties.

To update a search folder

  1. Retrieve a collection of all the search folders in a folder. In the following example, only the folders in the SearchFolder folder are retrieved. The FolderView is limited to 50.

    FindFoldersResults findResults = service.FindFolders(
        WellKnownFolderName.SearchFolders,
        new FolderView(50));
    
  2. Locate a specific search folder. The following code example shows how to retrieve a collection of search folders from the Exchange mailbox and to select the folder named "Extended". The SearchFolder must bind to the service to retrieve all the information in the search folder.

    foreach (SearchFolder searchFolder in findResults.Folders)
    {
        if (searchFolder.DisplayName == "Extended")
        {
    
  3. Instantiate the search folder.

    SearchFolder selectedsearchFolder = searchFolder;
    
  4. Define the new search filter to include "Using" in the subject.

    selectedsearchFolder.SearchParameters.SearchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Using");
    selectedsearchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
    
  5. Define the Dictionary properties to update. In this example, only the SearchFilter property is updated.

    Dictionary<PropertyDefinition, object> propertiesToUpdate = new Dictionary<PropertyDefinition, object>();
    propertiesToUpdate.Add(SearchFolderSchema.SearchParameters, "UPDATED:" + selectedsearchFolder.SearchParameters.SearchFilter);
    
  6. Update the mailbox store. This update replaces the SearchFilter that is located in the mailbox store.

    selectedsearchFolder.Update();
    
  7. Close the if statement and the foreach loop.

        }
    }
    

You can provide a new search filter to an existing search folder by using the Update method.

Example

The following code example shows how to retrieve a specific search folder with the display name "Extended". The code example searches each SearchFolder in the WellKnownFolderName.SearchFolders folder and displays any mail that contains the substring "Using" in the subject line.

// Find the search folder named "Extended".
FindFoldersResults findResults = service.FindFolders(
    WellKnownFolderName.SearchFolders,
    new FolderView(50));
foreach (SearchFolder searchFolder in findResults.Folders)
{
    if (searchFolder.DisplayName == "Extended")
    {
        SearchFolder selectedsearchFolder = searchFolder;
        
        // Define the searchFilter to include contains "Using" in the subject line.
        selectedsearchFolder.SearchParameters.SearchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Using");
        selectedsearchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);

        // Define the properties to update.
        Dictionary<PropertyDefinition, object> propertiesToUpdate = new Dictionary<PropertyDefinition, object>();
        propertiesToUpdate.Add(SearchFolderSchema.SearchParameters, "UPDATED:" + selectedsearchFolder.SearchParameters.SearchFilter);
        try
        {
            selectedsearchFolder.Update();
            Console.WriteLine(selectedsearchFolder.DisplayName + ": updated.");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error - " + e.Message);
        }
        break;
    }
}
' Find the search folder named "Extended".
Dim findResults As FindFoldersResults = _
    service.FindFolders(WellKnownFolderName.SearchFolders, New FolderView(50))
For Each searchFolder As SearchFolder In findResults.Folders
    If (searchFolder.DisplayName = "Extended") Then
        Dim selectedsearchFolder As SearchFolder = searchFolder

        ' Define the searchFilter to include contains 'Using' in the subject line.
        selectedsearchFolder.SearchParameters.SearchFilter = New SearchFilter.ContainsSubstring(ItemSchema.Subject, "Using")
        selectedsearchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox)

        ' Define the properties to update.
        Dim propertiesToUpdate As New Dictionary(Of PropertyDefinition, Object)
        propertiesToUpdate.Add(SearchFolderSchema.SearchParameters, selectedsearchFolder.SearchParameters.SearchFilter)

        Try
            selectedsearchFolder.Update()
            Console.WriteLine((selectedsearchFolder.DisplayName & ": updated."))
        Catch exception As Exception
            Console.WriteLine("Error - " & exception.Message)
        End Try
        Exit For
    End If
Next

The following example shows the XML request that is sent by the client to the server when a search folder is updated in an Exchange mailbox. The FolderId and ChangeKey attributes have been shortened to preserve readability.

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010" />
  </soap:Header>
  <soap:Body>
    <m:UpdateFolder>
      <m:FolderChanges>
        <t:FolderChange>
          <t:FolderId Id="AQMkAGEyYzQ" ChangeKey="CAAAABYAAA " />
          <t:Updates>
            <t:SetFolderField>
              <t:FieldURI FieldURI="folder:SearchParameters" />
              <t:SearchFolder>
                <t:SearchParameters Traversal="Shallow">
                  <t:Restriction>
                    <t:Contains ContainmentMode="Substring" 
                       ContainmentComparison="IgnoreCase">
                      <t:FieldURI FieldURI="item:Subject" />
                      <t:Constant Value="Using" />
                    </t:Contains>
                  </t:Restriction>
                  <t:BaseFolderIds>
                    <t:DistinguishedFolderId Id="inbox" />
                  </t:BaseFolderIds>
                </t:SearchParameters>
              </t:SearchFolder>
            </t:SetFolderField>
          </t:Updates>
        </t:FolderChange>
      </m:FolderChanges>
    </m:UpdateFolder>
  </soap:Body>
</soap:Envelope>

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

<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" 
        MinorVersion="0" 
        MajorBuildNumber="639" 
        MinorBuildNumber="20" 
        Version="Exchange2010" 
        xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm=""
        lns:xsd="http://www.w3.org/2001/XMLSchema" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <m:UpdateFolderResponse 
        xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:UpdateFolderResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Folders>
            <t:SearchFolder>
              <t:FolderId Id="AQMkAGEyYzQ" ChangeKey="CAAAABYAAA " />
            </t:SearchFolder>
          </m:Folders>
        </m:UpdateFolderResponseMessage>
      </m:ResponseMessages>
    </m:UpdateFolderResponse>
  </s:Body>
</s:Envelope>

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

Compiling the code

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

Robust programming

  • 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.

Security

  • 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.