Jaa


Creating Folders by using EWS in Exchange 2010

Last modified: May 21, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

You can use Exchange Web Services to create folders to organize items and other folders.

Example

The following example shows you how to create two folders in the Inbox. One of the folders that is created in this example is a Tasks folder.

static void CreateFolder(ExchangeServiceBinding esb)
{
    // Create the request.
    FolderType folder = new FolderType();
    FolderType folder2 = new FolderType();
    folder.DisplayName = "TestFolder";
    folder2.DisplayName = "TestFolder2";
    folder2.FolderClass = "IPF.Task";

    DistinguishedFolderIdType distFolder = new DistinguishedFolderIdType();
    distFolder.Id = DistinguishedFolderIdNameType.inbox;
    TargetFolderIdType targetID = new TargetFolderIdType();
    targetID.Item = distFolder;

    CreateFolderType createFolder = new CreateFolderType();
    createFolder.Folders = new FolderType[] { folder, folder2 };
    createFolder.ParentFolderId = targetID;

    try
    {
        // Send the request and get the response.
        CreateFolderResponseType response = esb.CreateFolder(createFolder);
        
        // Get the response messages.
        ResponseMessageType[] rmta = response.ResponseMessages.Items;

        foreach (ResponseMessageType rmt in rmta)
        {
            // Cast to the correct response message type.
            if (((FolderInfoResponseMessageType)rmt).ResponseClass == ResponseClassType.Success)
                Console.WriteLine("Folder added to mailbox.");
        }
    }

    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

The following XML example shows the request message that is sent from the client to the server.

<?xml version="1.0" encoding="utf-8"?>
<CreateFolder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ParentFolderId xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <DistinguishedFolderId Id="inbox" xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </ParentFolderId>
  <Folders xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <Folder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
      <DisplayName>TestFolder</DisplayName>
    </Folder>
    <Folder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
      <FolderClass>IPF.Task</FolderClass>
      <DisplayName>TestFolder2</DisplayName>
    </Folder>
  </Folders>
</CreateFolder>

The following XML example shows the XML response message that is sent from the server to the client.

<CreateFolderResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <CreateFolderResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <Folders>
        <Folder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <FolderId Id="AQApAH" ChangeKey="AQAAABY" />
        </Folder>
      </Folders>
    </CreateFolderResponseMessage>
    <CreateFolderResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <Folders>
        <TasksFolder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <FolderId Id="AQApAH" ChangeKey="BAAAABY" />
        </TasksFolder>
      </Folders>
    </CreateFolderResponseMessage>
  </ResponseMessages>
</CreateFolderResponse>

Note

The folder identifier and change key have been shortened to preserve readability.

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by MicrosoftVisual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.

You can also create a folder by using an XML message. For more information, see CreateFolder Operation.

Compiling the Code

For information about compiling the code, see EWS Client Development in Exchange 2010.

Robust Programming

The namespace that is created when the proxy is generated must also be added to this example.