次の方法で共有


Updating contacts by using the EWS Managed API 2.0

Find out how to use the EWS Managed API to update contacts.

Last modified: January 09, 2014

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

To update a contact

  1. Bind to an existing contact by using its unique identifier. The following code shows how to bind to an existing contact and provide it with connection configuration information by using an ExchangeService object named service. The ItemId has been shortened to preserve readability.

    Contact contact = Contact.Bind(service, new ItemId("AAMkA="));
    
  2. Update properties on the contact. The following code shows how to update the contact's surname, company name, business phone number, second email address, first instant messaging (IM) address, and business address.

    // Update the contact's surname and company name.
    contact.Surname = "Johnson";
    contact.CompanyName = "Contoso";
    
    // Update the contact's business phone number.
    contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0140";
    
    // Update the contact's second email address.
    contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("brian_2@contoso.com");
    
    // Update the contact's first IM address.
    contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";
    
    // Update the contact's business address.
    PhysicalAddressEntry PABusinessEntry = new PhysicalAddressEntry();
    PABusinessEntry.Street = "4567 Contoso Way";
    PABusinessEntry.City = "Redmond";
    PABusinessEntry.State = "OH";
    PABusinessEntry.PostalCode = "33333";
    PABusinessEntry.CountryOrRegion = "United States of America";
    contact.PhysicalAddresses[PhysicalAddressKey.Business] = PABusinessEntry;
    

    Note

    To update email addresses, IM addresses, physical addresses, or phone numbers for a contact, use the appropriate dictionary object and key. For example:

    • To update the first email address field for a contact, specify the value of contact.EmailAddresses[EmailAddressKey.EmailAddress1].

    • To update the first instant messaging address for a contact, specify the value of contact.ImAddresses[ImAddressKey.ImAddress1].

    • To update the home address for a contact, specify the value of contact.PhysicalAddresses[PhysicalAddressKey.Home].

    • To update the business phone number for a contact, specify the value of contact.PhoneNumbers[PhoneNumberKey.BusinessPhone].

    If you want to delete email address fields from a contact, read the EWS Managed API: How to remove Email1, Email2, Email3 from a contact blog post.

  3. Save the updated contact. The following code shows how to save an updated contact.

    contact.Update(ConflictResolutionMode.AlwaysOverwrite);
    

Example

The following code example shows how to update the surname, company name, business phone number, second email address, first IM address, and business address of a contact. The updated contact is saved in the Contacts folder. This example assumes that service is a valid ExchangeService binding. The ItemId has been shortened to preserve readability.

// Bind to an existing meeting request by using its unique identifier.
Contact contact = Contact.Bind(service, new ItemId("AAMkA="));

// Update the contact's surname and company name.
contact.Surname = "Johnson";
contact.CompanyName = "Contoso";

// Update the contact's business phone number.
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "444-444-4444";

// Update the contact's second email address.
contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("brian_2@contoso.com");

// Update the contact's first IM address.
contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";

// Update the contact's business address.
PhysicalAddressEntry PABusinessEntry = new PhysicalAddressEntry();
PABusinessEntry.Street = "4567 Contoso Way";
PABusinessEntry.City = "Redmond";
PABusinessEntry.State = "OH";
PABusinessEntry.PostalCode = "33333";
PABusinessEntry.CountryOrRegion = "United States of America";
contact.PhysicalAddresses[PhysicalAddressKey.Business] = PABusinessEntry;

// Save the contact.
contact.Update(ConflictResolutionMode.AlwaysOverwrite);

The following example shows the XML that is sent by using the Update(ConflictResolutionMode) method. The UniqueId() and ChangeKey() member values have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Eastern Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">
      <m:ItemChanges>
        <t:ItemChange>
          <t:ItemId Id="AAMkA=" ChangeKey="EQAAA" />
          <t:Updates>
            <t:SetItemField>
              <t:FieldURI FieldURI="contacts:Surname" />
              <t:Contact>
                <t:Surname>Johnson</t:Surname>
              </t:Contact>
            </t:SetItemField>
            <t:SetItemField>
              <t:FieldURI FieldURI="contacts:CompanyName" />
              <t:Contact>
                <t:CompanyName>Contoso</t:CompanyName>
              </t:Contact>
            </t:SetItemField>
            <t:SetItemField>
              <t:IndexedFieldURI FieldURI="contacts:EmailAddress" FieldIndex="EmailAddress2" />
              <t:Contact>
                <t:EmailAddresses>
                  <t:Entry Key="EmailAddress2">brian_2@contoso.com</t:Entry>
                </t:EmailAddresses>
              </t:Contact>
            </t:SetItemField>
          </t:Updates>
        </t:ItemChange>
      </m:ItemChanges>
    </m:UpdateItem>
  </soap:Body>
</soap:Envelope>

The following example shows the XML response that is returned when using the Update(ConflictResolutionMode) method. The UniqueId() and ChangeKey() member values have been shortened for readability.

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="731" MinorBuildNumber="10" Version="V2_3" 
        xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns="https://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:UpdateItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
        xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:UpdateItemResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Items>
            <t:Contact>
              <t:ItemId Id="AAMkA=" ChangeKey="EQAA" />
            </t:Contact>
          </m:Items>
          <m:ConflictResults>
            <t:Count>0</t:Count>
          </m:ConflictResults>
        </m:UpdateItemResponseMessage>
      </m:ResponseMessages>
    </m:UpdateItemResponse>
  </s:Body>
</s:Envelope>

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.