Creating Tasks (Exchange Web Services)

Topic Last Modified: 2009-07-24

You can use Exchange Web Services to create tasks in a user's mailbox.

Example

The following example shows you how to create a weekly recurring task.

Note

Starting with Exchange 2007 SP2, Exchange Web Services includes the ability to specify the time zone when you create task items. For more information, see Updating Exchange Web Services Clients for Exchange 2007 SP2.

public TaskType CreateRecurringTask()
{
    // Create the task item and set property values.
    TaskType task = new TaskType();
    task.Subject = "This is a recurring task that was created by using the autogenerated EWS proxies.";
    task.Body = new BodyType();
    task.Body.BodyType1 = BodyTypeType.Text;
    task.Body.Value = "This task occurs every week.";
    task.StartDate = DateTime.Now;
    task.StartDateSpecified = true;

    // Create the regeneration pattern.
    WeeklyRegeneratingPatternType regenerationPattern = new WeeklyRegeneratingPatternType();
    regenerationPattern.Interval = 1;

    // Define the recurrence pattern.
    NoEndRecurrenceRangeType recurrenceRange = new NoEndRecurrenceRangeType();
    recurrenceRange.StartDate = task.StartDate;

    // Set the regeneration and recurrence patterns.
    TaskRecurrenceType recurrence = new TaskRecurrenceType();
    recurrence.Item = regenerationPattern;
    recurrence.Item1 = recurrenceRange;

    task.Recurrence = recurrence;

    // Create the request to make a new task item.
    CreateItemType createItemRequest = new CreateItemType();
    createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
    createItemRequest.Items.Items = new ItemType[1];
    createItemRequest.Items.Items[0] = task;

    // Send the request and receive the response.
    CreateItemResponseType createItemResponse = this.Service.CreateItem(createItemRequest);

    // Access a response message.
    ItemInfoResponseMessageType responseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;

    // Return the new task item.
    return responseMessage.Items.Items[0] as TaskType;
}

public ExchangeServiceBinding Service
{
    get
    {
        if (this.service == null)
        {
            this.service = new ExchangeServiceBinding();
            this.service.Credentials = new NetworkCredential(
                this.UserName,
                this.Password,
                this.Domain);
            this.service.Url = this.Url; 

            // Set the request version.
            this.service.RequestServerVersionValue = new RequestServerVersion();
            this.service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

            // Specify the time zone for the DateTime properties of tasks.
            TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
            tzdt.Id = "Eastern Standard Time";
            TimeZoneContextType tzct = new TimeZoneContextType();
            tzct.TimeZoneDefinition = tzdt;
            this.service.TimeZoneContext = tzct;
        }
        return this.service;
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/"
               xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1"/>
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Eastern Standard Time"/>
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <CreateItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Items xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
          <Task xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
            <Subject>This is a recurring task that was created by using the autogenerated EWS proxies.</Subject>
            <Body BodyType="Text">This task occurs every week.</Body>
            <Recurrence>
              <WeeklyRegeneration>
                <Interval>1</Interval>
              </WeeklyRegeneration>
              <NoEndRecurrence>
                <StartDate>2006-08-15</StartDate>
              </NoEndRecurrence>
            </Recurrence>
            <StartDate>2006-08-15T14:24:51.3876635-07:00</StartDate>
          </Task>
      </Items>
    </CreateItem>
  </soap:Body>
</soap:Envelope>

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"?>
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="653" MinorBuildNumber="0" 
                         xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"/>
  </soap:Header>
  <soap:Body>
    <CreateItemResponse 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">
        <CreateItemResponseMessage ResponseClass="Success">
          <ResponseCode>NoError</ResponseCode>
          <Items>
            <Task xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
              <ItemId Id="AQApAHR" ChangeKey="EwAAABYA" />
            </Task>
          </Items>
        </CreateItemResponseMessage>
      </ResponseMessages>
    </CreateItemResponse>
  </soap:Body>
</soap:Envelope>

The ExchangeServiceBinding object that contains the user credentials and the location of the Exchange server that has the Client Access server role installed is encapsulated in this.Service.

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.

Note

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

Compiling the Code

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