Jaa


Creating 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 1.0 to create folders, calendar folders, contacts folders, and tasks folders in a mailbox.

To create a folder in a mailbox

  1. Create a generic folder with a valid ExchangeService object.

    Folder folder = new Folder(service);
    
  2. Set the folder properties. The following example shows how to set the DisplayName property.

    folder.DisplayName = "New folder";
    
  3. Save the new folder in a specified parent folder. The following example shows how to create the new folder as a child of the default Inbox folder.

    folder.Save(WellKnownFolderName.Inbox);
    

To create a calendar folder in a mailbox

  1. Create a CalendarFolder with a valid ExchangeService object.

    CalendarFolder folder = new CalendarFolder(service);
    
  2. Set the folder properties. The following example shows how to set the DisplayName property.

    folder.DisplayName = "New calendar folder";
    
  3. Save the new calendar folder in a specified parent folder. The following example shows how to create the new calendar folder as a child of the default Calendar folder.

    folder.Save(WellKnownFolderName.Calendar);
    

To create a contacts folder in a mailbox

  1. Create a ContactsFolder with a valid ExchangeService object.

    ContactsFolder folder = new ContactsFolder(service);
    
  2. Set the folder properties. The following example shows how to set the DisplayName property.

    folder.DisplayName = "New contacts folder";
    
  3. Save the new contacts folder in a specified parent folder. The following example shows how to create the new contacts folder as a child of the default Contacts folder.

    folder.Save(WellKnownFolderName.Contacts);
    

To create a tasks folder in a mailbox

  1. Create a TasksFolder with a valid ExchangeService object.

    TasksFolder folder = new TasksFolder(service);
    
  2. Set the folder properties. The following example shows how to set the DisplayName property.

    folder.DisplayName = "New tasks folder";
    
  3. Save the new tasks folder in a specified parent folder. The following example shows how to create the new tasks folder as a child of the default Tasks folder.

    folder.Save(WellKnownFolderName.Tasks);
    

The FolderClass property is automatically set when you create a calendar, contacts, tasks, search, or generic folder. To create a custom folder class, set the FolderClass property of the Folder object.

Example

The following code example shows how to create a folder with a custom folder class.

// Create a custom folder class.
Folder folder = new Folder(service);
folder.DisplayName = "Custom Folder";
folder.FolderClass = "IPF.MyCustomFolderClass";

// Create the folder as a child of the Inbox folder.
folder.Save(WellKnownFolderName.Drafts);
' Create a custom folder class.
Dim folder As New Folder(service)
folder.DisplayName = "Custom Folder"
folder.FolderClass = "IPF.MyCustomFolderClass"

' Create the folder as a child of the Inbox folder.
folder.Save(WellKnownFolderName.Drafts)

For an example that shows how to create search folders, see Creating search folders by using the EWS Managed API 2.0.

The following example shows the XML that is sent by the Save method.

<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:CreateFolder>
      <m:ParentFolderId>
        <t:DistinguishedFolderId Id="drafts" />
      </m:ParentFolderId>
      <m:Folders>
        <t:Folder>
          <t:FolderClass>IPF.MyCustomFolderClass</t:FolderClass>
          <t:DisplayName>Custom Folder</t:DisplayName>
          <t:PermissionSet>
            <t:Permissions />
          </t:PermissionSet>
        </t:Folder>
      </m:Folders>
    </m:CreateFolder>
  </soap:Body>
</soap:Envelope>

The following example shows the XML that is returned by using the Save method. 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:CreateFolderResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
               xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:CreateFolderResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Folders>
            <t:Folder>
              <t:FolderId Id="AQMkAGEyYzQ”
                  ChangeKey="AQAAABYA" />
            </t:Folder>
          </m:Folders>
        </m:CreateFolderResponseMessage>
      </m:ResponseMessages>
    </m:CreateFolderResponse>
  </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.