Creating Contacts by using EWS in Exchange 2010

Last modified: May 21, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

You can use Exchange Web Services to create contact items in a mailbox.


The following example shows you how create a contact item in the Exchange store.

static void CreateContact(ExchangeServiceBinding esb)
    // Create an object of create item type.
    CreateItemType createItemType = new CreateItemType();

    // Because you are creating a contact, save the item in the Contacts folder.
    createItemType.SavedItemFolderId = new TargetFolderIdType();
    DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();
    contactsFolder.Id = DistinguishedFolderIdNameType.contacts;

    createItemType.SavedItemFolderId.Item = contactsFolder;

    createItemType.Items = new NonEmptyArrayOfAllItemsType();
    createItemType.Items.Items = new ItemType[1];

    // Create a contact item type.
    ContactItemType contactItem = new ContactItemType();

    // Set the relevant properties on the contact.
    contactItem.FileAs = "Friend A";

    // Set the contact name and job information.
    contactItem.GivenName = "Don";
    contactItem.Surname = "Hall";
    contactItem.CompanyName = "AdventureWorks";
    contactItem.JobTitle = "Software Engineer";

    // Set a single e-mail address for the contact.
    contactItem.EmailAddresses = new EmailAddressDictionaryEntryType[1];
    EmailAddressDictionaryEntryType address = new EmailAddressDictionaryEntryType();
    address.Key = EmailAddressKeyType.EmailAddress1;
    address.Value = "";
    contactItem.EmailAddresses[0] = address;

    // Set a single contact physical address.
    contactItem.PhysicalAddresses = new PhysicalAddressDictionaryEntryType[1];
    PhysicalAddressDictionaryEntryType physicalAddress = new PhysicalAddressDictionaryEntryType();
    physicalAddress.Key = PhysicalAddressKeyType.Home;
    physicalAddress.Street = "1234 56 Ave NE";
    physicalAddress.City = "La Habra Heights";
    physicalAddress.Country = "United States";
    physicalAddress.PostalCode = "98072";

    contactItem.PhysicalAddresses[0] = physicalAddress;

    // Set the contact telephone number.
    contactItem.PhoneNumbers = new PhoneNumberDictionaryEntryType[1];
    PhoneNumberDictionaryEntryType phoneEntry = new PhoneNumberDictionaryEntryType();
    phoneEntry.Key = PhoneNumberKeyType.BusinessPhone;
    phoneEntry.Value = "5625550100";

    contactItem.PhoneNumbers[0] = phoneEntry;

    createItemType.Items.Items[0] = contactItem;

    // Send the request to create the contact item; receive the response.
    CreateItemResponseType createItemResponse = esb.CreateItem(createItemType);

    // Check the results of the request.
    if (createItemResponse.ResponseMessages.Items.Length > 0 &&
        createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
        ItemInfoResponseMessageType responseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
        ContactItemType contactResponse = responseMessage.Items.Items[0] as ContactItemType;
        Console.WriteLine("Created Contact Item with Id {0} and ChangeKey {1}", contactResponse.ItemId.Id, contactResponse.ItemId.ChangeKey);

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

<?xml version="1.0" encoding="utf-8"?>
<CreateItem xmlns:xsi="" 
  <SavedItemFolderId xmlns="">
    <DistinguishedFolderId Id="contacts" xmlns="" />
  <Items xmlns="">
    <Contact xmlns="">
      <FileAs>Friend A</FileAs>
        <Entry Key="EmailAddress1"></Entry>
        <Entry Key="Home">
          <Street>1234 56 Ave NE</Street>
          <City>La Habra Heights</City>
          <Country>United States</Country>
        <Entry Key="BusinessPhone">5625550100</Entry>
      <JobTitle>Software Engineer</JobTitle>

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

<?xml version="1.0" encoding="utf-8"?>
<CreateItemResponse xmlns:xsi="" 
  <ResponseMessages xmlns="">
    <CreateItemResponseMessage ResponseClass="Success">
        <Contact xmlns="">
          <ItemId Id="AAAtA=" ChangeKey="EQAAABYA" />

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.


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.