Updating Tasks in Exchange 2010

Last modified: March 16, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

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

Example

The following example shows you how to update a task. The task is updated with an appended body and has the status set to completed. If the task is recurring, a one-off task is generated to represent the completed occurrence.

public TaskType MarkTaskAsComplete(
    string taskId,
    string taskChangeKey)
{
    // Create the identifier of the task item to update.
    ItemIdType itemId = new ItemIdType();
    itemId.Id = taskId;
    itemId.ChangeKey = taskChangeKey;

    // Create a task item to hold a set update.
    TaskType setStatusTask = new TaskType();
    setStatusTask.Status = TaskStatusType.Completed;
    setStatusTask.StatusSpecified = true;

    // Create the set update.
    SetItemFieldType setItemField = new SetItemFieldType();
    setItemField.Item = new PathToUnindexedFieldType();
    (setItemField.Item as PathToUnindexedFieldType).FieldURI = UnindexedFieldURIType.taskStatus;
    setItemField.Item1 = setStatusTask;

    // Create a task item to hold an append update.
    TaskType appendToBodyTask = new TaskType();
    appendToBodyTask.Body = new BodyType();
    appendToBodyTask.Body.BodyType1 = BodyTypeType.Text;
    appendToBodyTask.Body.Value = "Adding some text to the body of this task. ";

    // Create the append update.
    AppendToItemFieldType appendToItemField = new AppendToItemFieldType();
    appendToItemField.Item = new PathToUnindexedFieldType();
    (appendToItemField.Item as PathToUnindexedFieldType).FieldURI = UnindexedFieldURIType.itemBody;
    appendToItemField.Item1 = appendToBodyTask;

    // Create the update request.
    UpdateItemType updateItemRequest = new UpdateItemType();
    updateItemRequest.ItemChanges = new ItemChangeType[1];
    ItemChangeType itemChange = new ItemChangeType();
    itemChange.Item = itemId;
    itemChange.Updates = new ItemChangeDescriptionType[2];
    itemChange.Updates[0] = setItemField;
    itemChange.Updates[1] = appendToItemField;
    updateItemRequest.ItemChanges[0] = itemChange;

    // Send the update request and receive the update response.
    UpdateItemResponseType updateItemResponse = this.Service.UpdateItem(updateItemRequest);

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

    // Return the updated task item identifier.
    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;
    }
}

This method takes as arguments the item identifier and change key of the task to update. This method returns a Task object.

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 following XML example shows the XML request message that is sent from the client to the server.

<UpdateItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            ConflictResolution="NeverOverwrite">
  <ItemChanges xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <ItemChange xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
      <ItemId Id="AQApAH" ChangeKey="EwAAABYr" />
      <Updates>
        <SetItemField>
          <FieldURI FieldURI="task:Status" />
          <Task>
            <Status>Completed</Status>
          </Task>
        </SetItemField>
        <AppendToItemField>
          <FieldURI FieldURI="item:Body" />
          <Task>
            <Body BodyType="Text">Adding some text to the body of this task. </Body>
          </Task>
        </AppendToItemField>
      </Updates>
    </ItemChange>
  </ItemChanges>
</UpdateItem>

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

<UpdateItemResponse 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">
    <UpdateItemResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <Items>
        <Task xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <ItemId Id="AQApAH" ChangeKey="EwAAABYv" />
        </Task>
      </Items>
    </UpdateItemResponseMessage>
  </ResponseMessages>
</UpdateItemResponse>

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. Item identifiers are returned in the FindItem, CreateAttachment, CreateItem (when the item is saved and not sent), GetAttachment, GetEvents, GetItem, and UpdateItem responses.

Compiling the Code

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