Updating Tasks (Exchange Web Services)

Topic Last Modified: 2009-07-27

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


The following example shows 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.


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

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
        if (this.service == null)
            this.service = new ExchangeServiceBinding();
            this.service.Credentials = new NetworkCredential(
            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;

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 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"
    <t:RequestServerVersion Version="Exchange2007_SP1"/>
      <t:TimeZoneDefinition Id="Eastern Standard Time"/>
    <UpdateItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      <ItemChanges xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
        <ItemChange xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
          <ItemId Id="AQApAH" ChangeKey="EwAAABYr" />
              <FieldURI FieldURI="task:Status" />
              <FieldURI FieldURI="item:Body" />
                <Body BodyType="Text">Adding some text to the body of this task. </Body>

The following 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="http://schemas.xmlsoap.org/soap/envelope/" 
    <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="653" MinorBuildNumber="0" 
    <UpdateItemResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      <ResponseMessages xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
        <UpdateItemResponseMessage ResponseClass="Success">
            <Task xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
              <ItemId Id="AQApAH" ChangeKey="EwAAABYv" />

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.


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 Exchange Web Services Client Development.