static void CreateContact()
{
// Create the bindings and set the credentials.
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = "http://fqdn/ews/exchange.asmx";
esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
// 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 = "don@example.com";
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);
}
}