问题
I try to update a contact property in EWS with an empty string but it fails. I have no idea why.
// works fine
contact.Company = "SomeCompany";
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
// failed in Update with a service response exception
contact.Company = "";
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
I try null and string.Empty but its the same effect. What am I doing wrong?
回答1:
Are you sure there is a Company
property on the Contact type? I only see a CompanyName property which can be updated in the following way without any problems (at least when I try it):
var service = GetService();
var view = new ItemView(1);
var searchFilter = new SearchFilter.IsEqualTo(ContactSchema.EmailAddress1, "test@domain.dk");
var contacts = service.FindItems(WellKnownFolderName.Contacts, searchFilter, view);
var contact = contacts.ElementAt(0) as Contact;
// Works fine.
contact.CompanyName = "SomeCompany";
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
// Works fine as well.
contact.CompanyName = "";
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
回答2:
Firstly, you can update more than one property at a time.
Secondly, you can try to check if the existing value is the same as the new value (if it is, don't set it). Also, it's better to set the value to null if it's an empty string.
Thirdly, find your contact, get the item ID and then bind a new Contact to that item ID.
ItemId itemId = contact.Id;
Contact updateContact = Contact.Bind(service, itemId);
This may help. Who knows. The API is so difficult to work with when updating an item.
Check this link where I've put a lot of information on updating a contact using EWS: Updating a Contact with EWS
来源:https://stackoverflow.com/questions/9480467/how-i-can-update-a-contact-item-in-exchange-web-api