Compartir a través de


Creating Tasks in Exchange 2010

Last modified: March 16, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

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.

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;
        }
        return this.service;
    }
}

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

<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>

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

<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>

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

Note

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

Compiling the Code

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