Sdílet prostřednictvím


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.

Example

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.

Note

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
{
    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;
    }
}

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"
               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>
    <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>
  </soap:Body>
</soap:Envelope>

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="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>
    <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>
  </soap:Body>
</soap:Envelope>

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