我一直在努力寻找如何与EWS合作,并设法找出如何做我想做的一切。我使用EWS来管理自定义数据库和Exchange服务器之间的联系(2007)。添加和删除工作正常,但我无法更新联系人。为了解决这个问题,我删除了联系人并重新创建了它,但是当通过Outlook(或其他任何东西)编辑联系人时会出现问题。
我试着按照这个链接说的那样:
http://msdn.microsoft.com/en-us/library/exchange/ee693002(v=exchg.80).aspx
但是我收到一条错误消息,说只能更新一个属性。然后我一次更新一个属性,一旦我尝试更新电话号码,我就会收到一条错误消息:“命名空间中的'更新'元素'http://schemas.microsoft.com/exchange/services / 2006 / types'内容不完整。“
这是代码,基本上是:
ItemId itemId = contactToUpdate.Id;
Contact updateContact = Contact.Bind(service, itemId);
updateContact.PhoneNumbers[PhoneNumberKey.HomePhone] = customContact.HomeTelephone;
updateContact.Update(ConflictResolutionMode.AlwaysOverwrite);
有谁知道我为什么会收到这个错误?任何人都可以回答如何实际更新项目?我有错过的文件吗?
EWS dll的版本是15.0.516.12。
答案 0 :(得分:9)
经过多次调试后我找到了答案。我不会去探索更多以找出原因,但你不能这样做:
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);
但是,您可以只设置一个电话号码,然后在调试时更改其他两个的值(在断点处)。怪异。
无论如何,有一个问题 - 如果值没有改变(这就是答案),你就无法更新其中一个字典值,所以先检查一下这个值是否有所改变。接下来,如果值是空字符串,则需要将其另存为null。您还需要在尝试读取其值之前检查字典条目是否存在。
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);
}
更新地址:
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);
在我看来,这是API的粗心实施(以及糟糕的文档)。
需要对项目的每个属性执行更新的事实可能是由于服务器运行的是Exchange 2007,但这尚未得到确认。
我尝试使用相同结果的Microsoft.Exchange.WebServices版本14和15。
<强>更新强>
但等等,还有更多。
我发布了很多代码来说明如何更新不同的电话号码,地址等。该代码还显示了如何更新(或设置)标题,性别,后缀和自定义属性(用户定义的字段 - 如果要在Outlook中显示,则需要将用户定义的字段添加到contacts文件夹中。)
这是一段完整的,有效的代码:
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;
// Extended properties -------------------------------------------------------------
ExtendedPropertyDefinition extendedPropertyDefinition;
// Gender
if (!string.IsNullOrEmpty(customContact.Gender))
{
extendedPropertyDefinition = new ExtendedPropertyDefinition(0x3a4d, MapiPropertyType.Short);
switch (customContact.Gender)
{
case "Male":
updateContact.SetExtendedProperty(extendedPropertyDefinition, 2);
break;
case "Female":
updateContact.SetExtendedProperty(extendedPropertyDefinition, 1);
break;
default:
updateContact.SetExtendedProperty(extendedPropertyDefinition, 3);
break;
}
}
// Title
if (!string.IsNullOrEmpty(customContact.Title))
{
extendedPropertyDefinition = new ExtendedPropertyDefinition(0x3a45, MapiPropertyType.String);
updateContact.SetExtendedProperty(extendedPropertyDefinition, customContact.Title);
}
// Suffix
if (!string.IsNullOrEmpty(customContact.Suffix))
{
extendedPropertyDefinition = new ExtendedPropertyDefinition(0x3a05, MapiPropertyType.String);
updateContact.SetExtendedProperty(extendedPropertyDefinition, customContact.Suffix);
}
// Custom property
extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "customProperty", MapiPropertyType.String);
updateContact.SetExtendedProperty(extendedPropertyDefinition, customContact.CustomProperty);
// File the contact
updateContact.Subject = customContact.Name + " " + customContact.Surname;
updateContact.DisplayName = customContact.Name + " " + customContact.Surname;
updateContact.Update(ConflictResolutionMode.AlwaysOverwrite);
答案 1 :(得分:0)
您是否尝试过使用Microsoft在their example中使用的方式来更新商家地址:
PhysicalAddressEntry PABusinessEntry = new PhysicalAddressEntry();
PABusinessEntry.Street = "4567 Contoso Way";
PABusinessEntry.City = "Redmond";
PABusinessEntry.State = "OH";
PABusinessEntry.PostalCode = "33333";
PABusinessEntry.CountryOrRegion = "United States of America";
contact.PhysicalAddresses[PhysicalAddressKey.Business] = PABusinessEntry;
这样就创建了一个新的PhysicalAddressEntry-object,它可以解决你的问题。
(当然这对电话号码没有帮助。)