I found the answer to this after much debugging. I am not going to research more to find out why, but you cannot do this:
updateContact = Contact.Bind(service, itemId); updateContact.PhoneNumbers[PhoneNumberKey.HomePhone] = customContact.HomePhone; updateContact.PhoneNumbers[PhoneNumberKey.MobilePhone] = customContact.MobilePhone; updateContact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = customContact.BusinessPhone; updateContact.Update(ConflictResolutionMode.AlwaysOverwrite);
However, you can only set one phone number and then change the values ββfor the other two during debugging (while at the breakpoint). Weird
In any case, there is a problem: you cannot update one of these dictionary values ββif the value does not change (this is a short answer), so first check to see if the value has changed. Then, if the value is an empty string, you should rather save it as null. You also need to check if a dictionary entry exists before trying to read its meaning.
string number; bool numberFound; numberFound = updateContact.PhoneNumbers.TryGetValue(PhoneNumberKey.HomePhone, out number); if ((numberFound && number != customContact.HomePhone) || (!numberFound && !string.IsNullOrEmpty(customContact.HomePhone))) { updateContact = Contact.Bind(service, itemId); updateContact.PhoneNumbers[PhoneNumberKey.HomePhone] = customContact.HomePhone == "" ? null : customContact.HomePhone; updateContact.Update(ConflictResolutionMode.AlwaysOverwrite); }
To update the address:
updateContact = Contact.Bind(service, itemId); updateContact.PhysicalAddresses[PhysicalAddressKey.Home].Street = customContact.Street == "" ? null : customContact.Street; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].State = customContact.Suburb == "" ? null : customContact.Suburb; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].City = customContact.City == "" ? null : customContact.City; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion = customContact.Country == "" ? null : customContact.Country; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode = customContact.AreaCode == "" ? null : customContact.AreaCode; updateContact.Update(ConflictResolutionMode.AlwaysOverwrite);
This, in my opinion, is a careless implementation of the API (and poor documentation).
The fact that an update must be performed for each item property may be related to the Exchange 2007 server, but this has not yet been confirmed.
I tried both with versions 14 and 15 Microsoft.Exchange.WebServices with the same results.
Update
But wait, there still.
I post a lot of code to illustrate how to update different phone numbers, addresses, etc. The code also shows how to update (or set) the header, gender, suffix and custom property (user field, which if you want to appear in Outlook, you need to add the field specified by the user to the contacts folder).
Here is the full working code:
Contact updateContact = Contact.Bind(service, itemId); updateContact.GivenName = customContact.Name; updateContact.Surname = customContact.Surname; EmailAddress emailAddress; bool emailAddressFound; emailAddressFound = updateContact.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, out emailAddress); if (emailAddressFound && emailAddress.Address != customContact.Email) { emailAddress.Address = customContact.Email == "" ? null : customContact.Email; } else if (!emailAddressFound && !string.IsNullOrEmpty(customContact.Email)) { updateContact.EmailAddresses[EmailAddressKey.EmailAddress1] = customContact.Email; } updateContact.Initials = customContact.Initials; string number; bool numberFound; numberFound = updateContact.PhoneNumbers.TryGetValue(PhoneNumberKey.HomePhone, out number); if ((numberFound && number != customContact.HomePhone) || (!numberFound && !string.IsNullOrEmpty(customContact.HomePhone))) { updateContact.PhoneNumbers[PhoneNumberKey.HomePhone] = customContact.HomePhone == "" ? null : customContact.HomePhone; } numberFound = updateContact.PhoneNumbers.TryGetValue(PhoneNumberKey.MobilePhone, out number); if ((numberFound && number != customContact.CellPhone) || (!numberFound && !string.IsNullOrEmpty(customContact.CellPhone))) { updateContact.PhoneNumbers[PhoneNumberKey.MobilePhone] = customContact.CellPhone == "" ? null : customContact.CellPhone; } numberFound = updateContact.PhoneNumbers.TryGetValue(PhoneNumberKey.BusinessPhone, out number); if ((numberFound && number != customContact.WorkPhone) || (!numberFound && !string.IsNullOrEmpty(customContact.WorkPhone))) { updateContact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = customContact.WorkPhone == "" ? null : customContact.WorkPhone; } PhysicalAddressEntry phsicalAddress; bool phsicalAddressFound; int phsicalAddressLength = (customContact.Street + customContact.Suburb + customContact.City + customContact.Country + customContact.AreaCode).Length; phsicalAddressFound = updateContact.PhysicalAddresses.TryGetValue(PhysicalAddressKey.Home, out phsicalAddress); if (phsicalAddressFound) { updateContact.PhysicalAddresses[PhysicalAddressKey.Home].Street = customContact.Street == "" ? null : customContact.Street; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].State = customContact.Suburb == "" ? null : customContact.Suburb; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].City = customContact.City == "" ? null : customContact.City; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion = customContact.Country == "" ? null : customContact.Country; updateContact.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode = customContact.AreaCode == "" ? null : customContact.AreaCode; } else if (!phsicalAddressFound && phsicalAddressLength > 0) { updateContact.PhysicalAddresses[PhysicalAddressKey.Home] = homeAddress; } phsicalAddressLength = (customContact.BusinessStreet + customContact.BusinessSuburb + customContact.BusinessCity + customContact.BusinessCountry + customContact.BusinessAreaCode).Length; phsicalAddressFound = updateContact.PhysicalAddresses.TryGetValue(PhysicalAddressKey.Business, out phsicalAddress); if (phsicalAddressFound) { updateContact.PhysicalAddresses[PhysicalAddressKey.Business].Street = customContact.BusinessStreet == "" ? null : customContact.BusinessStreet; updateContact.PhysicalAddresses[PhysicalAddressKey.Business].State = customContact.BusinessSuburb == "" ? null : customContact.BusinessSuburb; updateContact.PhysicalAddresses[PhysicalAddressKey.Business].City = customContact.BusinessCity == "" ? null : customContact.BusinessCity; updateContact.PhysicalAddresses[PhysicalAddressKey.Business].CountryOrRegion = customContact.BusinessCountry == "" ? null : customContact.BusinessCountry; updateContact.PhysicalAddresses[PhysicalAddressKey.Business].PostalCode = customContact.BusinessAreaCode == "" ? null : customContact.BusinessAreaCode; } else if (!phsicalAddressFound && phsicalAddressLength > 0) { updateContact.PhysicalAddresses[PhysicalAddressKey.Business] = businessAddress; } updateContact.Birthday = customContact.Birthdate; updateContact.WeddingAnniversary = customContact.MaritalStatusDate;