Creating contacts by using the EWS Managed API 2.0
Find out how to use the Exchange Web Services (EWS) Managed API to create contacts.
Last modified: December 20, 2012
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 create a contact
Instantiate the Contact object and identify the Exchange service. The following code shows how to create an appointment and provide it with connection configuration information by using an ExchangeService object named service.
Contact contact = new Contact(service);
Set properties on the contact. The following example shows how to add a name, file as mapping, company name, telephone numbers, email addresses, instant messaging (IM) addresses, and physical addresses to a contact.
// Specify the name and how the contact should be filed. contact.GivenName = "Brian"; contact.MiddleName = "David"; contact.Surname = "Johnson"; contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName; // Specify the company name. contact.CompanyName = "Contoso"; // Specify the business, home, and car phone numbers. contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110"; contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120"; contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130"; // Specify two email addresses. contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("brian_1@contoso.com"); contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("brian_2@contoso.com"); // Specify two IM addresses. contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com"; contact.ImAddresses[ImAddressKey.ImAddress2] = "brianIM2@contoso.com"; // Specify the home address. PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry(); paEntry1.Street = "123 Main Street"; paEntry1.City = "Seattle"; paEntry1.State = "WA"; paEntry1.PostalCode = "11111"; paEntry1.CountryOrRegion = "United States"; contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1; // Specify the business address. PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry(); paEntry2.Street = "456 Corp Avenue"; paEntry2.City = "Seattle"; paEntry2.State = "WA"; paEntry2.PostalCode = "11111"; paEntry2.CountryOrRegion = "United States"; contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2;
Note
To set email addresses, instant messaging (IM) addresses, physical addresses, or telephone numbers for a contact, use the appropriate dictionary object and key. For example:
-
To set the first email address field for a contact, specify the value of contact.EmailAddresses[EmailAddressKey.EmailAddress1].
-
To set the first IM address for a contact, specify the value of contact.ImAddresses[ImAddressKey.ImAddress1].
-
To set the home address for a contact, specify the value of contact.PhysicalAddresses[PhysicalAddressKey.Home].
-
To set the business telephone number for a contact, specify the value of contact.PhoneNumbers[PhoneNumberKey.BusinessPhone].
-
Save the contact. The following code shows how to save a contact to the Contacts folder.
contact.Save();
Example
The following example shows how to create a new contact. This example assumes that service is a valid ExchangeService binding.
// Create the contact.
Contact contact = new Contact(service);
// Specify the name and how the contact should be filed.
contact.GivenName = "Brian";
contact.MiddleName = "David";
contact.Surname = "Johnson";
contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;
// Specify the company name.
contact.CompanyName = "Contoso";
// Specify the business, home, and car phone numbers.
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110";
contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120";
contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130";
// Specify two email addresses.
contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("brian_1@contoso.com");
contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("brian_2@contoso.com");
// Specify two IM addresses.
contact.ImAddresses[ImAddressKey.ImAddress1] = "brianIM1@contoso.com";
contact.ImAddresses[ImAddressKey.ImAddress2] = " brianIM2@contoso.com";
// Specify the home address.
PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry();
paEntry1.Street = "123 Main Street";
paEntry1.City = "Seattle";
paEntry1.State = "WA";
paEntry1.PostalCode = "11111";
paEntry1.CountryOrRegion = "United States";
contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1;
// Specify the business address.
PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry();
paEntry2.Street = "456 Corp Avenue";
paEntry2.City = "Seattle";
paEntry2.State = "WA";
paEntry2.PostalCode = "11111";
paEntry2.CountryOrRegion = "United States";
contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2;
// Save the contact.
contact.Save();
The following example shows the XML that is sent by using the Save method.
<?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="Exchange2010" />
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SaveOnly">
<m:Items>
<t:Contact>
<t:FileAsMapping>LastCommaFirst</t:FileAsMapping>
<t:GivenName>Brian</t:GivenName>
<t:MiddleName>David</t:MiddleName>
<t:CompanyName>Contoso</t:CompanyName>
<t:EmailAddresses>
<t:Entry Key="EmailAddress1">brian_1@contoso.com</t:Entry>
<t:Entry Key="EmailAddress2">brian_2@contoso.com</t:Entry>
</t:EmailAddresses>
<t:PhysicalAddresses>
<t:Entry Key="Home">
<t:Street>123 Main Street</t:Street>
<t:City>Seattle</t:City>
<t:State>WA</t:State>
<t:CountryOrRegion>United States</t:CountryOrRegion>
<t:PostalCode>11111</t:PostalCode>
</t:Entry>
<t:Entry Key="Business">
<t:Street>456 Corp Avenue</t:Street>
<t:City>Seattle</t:City>
<t:State>WA</t:State>
<t:CountryOrRegion>United States</t:CountryOrRegion>
<t:PostalCode>11111</t:PostalCode>
</t:Entry>
</t:PhysicalAddresses>
<t:PhoneNumbers>
<t:Entry Key="BusinessPhone">425-555-0110</t:Entry>
<t:Entry Key="HomePhone">425-555-0120</t:Entry>
<t:Entry Key="CarPhone">425-555-0130</t:Entry>
</t:PhoneNumbers>
<t:ImAddresses>
<t:Entry Key="ImAddress1">brianIM1@contoso.com</t:Entry>
<t:Entry Key="ImAddress2">brianIM2@contoso.com</t:Entry>
</t:ImAddresses>
<t:Surname>Johnson</t:Surname>
</t:Contact>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>
The following example shows the XML that is returned by using the Save method. The ItemId and ChangeKey attributes have been shortened to preserve readability.
<?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="14" MinorVersion="0" MajorBuildNumber="639" MinorBuildNumber="11" Version="Exchange2010"
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" />
</soap:Header>
<soap:Body>
<m:CreateItemResponse
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages">
<m:ResponseMessages>
<m:CreateItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Items>
<t:Contact>
<t:ItemId Id="AAMkA=" ChangeKey="DwAAA" />
</t:Contact>
</m:Items>
</m:CreateItemResponseMessage>
</m:ResponseMessages>
</m:CreateItemResponse>
</soap:Body>
</soap: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.