Jaa


Emptying folders by using the EWS Managed API 2.0

Last modified: October 13, 2012

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

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 empty folders in a mailbox. Optionally, you can also delete the subfolders of the specified folder.

To empty a folder in a mailbox

  1. Bind to the folder to empty. In the following example, the FolderId object named folderId identifies the folder to empty. The ExchangeService object named service contains the settings to bind to Exchange Web Services.

    Folder folder = Folder.Bind(service, folderId);
    
  2. Empty the bound folder. The following example shows how to empty the folder by using the deleteMode option and how to delete subfolders by using the deleteSubFolders option.

    folder.Empty(DeleteMode.HardDelete, true);
    

    The following tables list all the Empty(DeleteMode, Boolean) method parameters that you can use to empty a folder.

    deleteMode parameter option

    Action

    HardDelete

    The item or folder is permanently removed from the store.

    MoveToDeletedItems

    The item or folder is moved to the Deleted Items folder.

    SoftDelete

    The item or folder is moved to the dumpster if the dumpster is enabled.

    deleteSubFolders parameter option

    Action

    true

    Subfolders of the identified folders are deleted, along with the messages in the subfolder.

    false

    Subfolders of the identified folders are not deleted or emptied.

Example

The following example shows how to empty a folder. In this example, the subfolders are deleted. The FolderId object named folderId identifies the folder to empty.

// Empty the identified folder. Delete the subfolders within the identified folder.
Folder folder = Folder.Bind(service, folderId);
folder.Empty(DeleteMode.HardDelete, true);
'Empty the identified folder. Delete the subfolders within the identified folder.
Dim folderId As FolderId
Dim folder As Folder
folder = folder.Bind(service, folderId)
folder.Empty(DeleteMode.HardDelete, True)

The following example shows the XML that is sent by the EmptyFolder method. 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_SP1" />
  </soap:Header>
  <soap:Body>
    <m:EmptyFolder DeleteType="HardDelete" DeleteSubFolders="true">
      <m:FolderIds>
        <t:FolderId Id="AQMkADhhOGU0M" ChangeKey="AQAAABYAAABs" />
      </m:FolderIds>
    </m:EmptyFolder>
  </soap:Body>
</soap:Envelope>

The following example shows the XML that is returned by using the EmptyFolder method.

<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" 
        MinorVersion="1" 
        MajorBuildNumber="164" 
        MinorBuildNumber="0" 
        Version="Exchange2010_SP1" 
        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" 
        xmlns: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:EmptyFolderResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:EmptyFolderResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
        </m:EmptyFolderResponseMessage>
      </m:ResponseMessages>
    </m:EmptyFolderResponse>
  </s:Body>
</s:Envelope>

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.