Adding Managed Folders (Exchange Web Services)

Topic Last Modified: 2007-09-21

You can Use Exchange Web Services to subscribe users to managed custom folders.

Example

The following example shows you how to add two managed custom folders to a mailbox. The managed custom folders must already exist in the Active Directory directory service before you can add them to a mailbox.

static void CreateManagedFolder()
{
    // Create the binding and set the credentials.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
    esb.Url = @"http://exchangeServer.example.com/EWS/exchange.asmx";

    // Create the request.
    CreateManagedFolderRequestType mfreq = new CreateManagedFolderRequestType();

    // Identify the managed folder to add to the user mailbox.
    mfreq.FolderNames = new string[] { "Human Resources", "Research" };

    try
    {
        // Send the request and get the response.
        CreateManagedFolderResponseType mfresp = esb.CreateManagedFolder(mfreq);

        // Get the response messages.
        ResponseMessageType[] rmta = mfresp.ResponseMessages.Items;

        foreach (ResponseMessageType rmt in rmta)
        {
            // Cast to the correct response message type.
            if (((FolderInfoResponseMessageType)rmt).ResponseClass == ResponseClassType.Success)
                Console.WriteLine("Managed folder added to mailbox.");
        }
    }
    
    catch (Exception e)
    {
        Console.WriteLine("Exception: " + e.Message);
    }
}

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

<?xml version="1.0" encoding="utf-8" ?>
 <CreateManagedFolder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <FolderNames xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
     <FolderName xmlns="https://schemas.microsoft.com/exchange/services/2006/types">Human Resources</FolderName>
     <FolderName xmlns="https://schemas.microsoft.com/exchange/services/2006/types">Research</FolderName>
   </FolderNames>
 </CreateManagedFolder>

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

<?xml version="1.0" encoding="utf-8" ?>
<CreateManagedFolderResponse xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" 
                              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <CreateManagedFolderResponseMessage ResponseClass="Success">
     <ResponseCode>NoError</ResponseCode>
      <Folders>
        <Folder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <FolderId Id="AAAp=" ChangeKey="AQAAAB" />
        </Folder>
      </Folders>
    </CreateManagedFolderResponseMessage>
     <CreateManagedFolderResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
       <Folders>
         <Folder xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <FolderId Id="AAApA=" ChangeKey="AQAAABY" />
        </Folder>
      </Folders>
    </CreateManagedFolderResponseMessage>
  </ResponseMessages>
</CreateManagedFolderResponse>

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 Microsoft Visual 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.

Managed folders cannot be moved, renamed, or deleted unless the folder policy is turned off. It is important to maintain the names and order of the managed folders that are added to the request because the response does not return the display names of the folders that are added to a mailbox. The response returns only the folder identifiers in the order in which they are created in the request.

Compiling the Code

For information about compiling the code, see Exchange Web Services Client Development.

Robust Programming

For information about robust programming, see Exchange Web Services Architecture.