Exchange Web服务 - 更新联系人时出现错误代码ErrorIncorrectUpdatePropertyCount

时间:2012-03-23 18:27:01

标签: c#-4.0 exchangewebservices

我使用fallowing方法通过EWS更新Exchange中的联系人:

    private void UpdateContact(Microsoft.Exchange.WebServices.Data.Contact exContact, ContactInfo contact) {
        var pathList = new List<string>();
        try {
            log.DebugFormat("Process ExchangeContact '{0}' with Contact '{1}'", (exContact.IsNew ? "<new>" : exContact.DisplayName), contact.Id);

            exContact.GivenName = contact.AdditionalName;
            exContact.Surname = contact.Name;
            exContact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;

            exContact.CompanyName = "";
            if (contact is PersonInfo) {
                var person = (PersonInfo) contact;
                if (person.PersonCompany != null && person.PersonCompany.Company != null) {
                    exContact.CompanyName = person.PersonCompany.Company.DisplayName;
                }
                exContact.ImAddresses[ImAddressKey.ImAddress1] = person.MessengerName;
                exContact.ImAddresses[ImAddressKey.ImAddress2] = person.SkypeName;
            }

            // Specify the business, home, and car phone numbers.
            var comm = contact.GetCommunication(Constants.CommunicationType.PhoneBusiness);
            exContact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = (comm != null ? comm.Value : null);
            comm = contact.GetCommunication(Constants.CommunicationType.PhonePrivate);
            exContact.PhoneNumbers[PhoneNumberKey.HomePhone] = (comm != null ? comm.Value : null);
            comm = contact.GetCommunication(Constants.CommunicationType.PhoneMobile);
            exContact.PhoneNumbers[PhoneNumberKey.MobilePhone] = (comm != null ? comm.Value : null);

            comm = contact.GetCommunication(Constants.CommunicationType.MailBusiness);
            exContact.EmailAddresses[EmailAddressKey.EmailAddress1] = (comm != null ? new EmailAddress(comm.Value) : null);
            comm = contact.GetCommunication(Constants.CommunicationType.MailPrivate);
            exContact.EmailAddresses[EmailAddressKey.EmailAddress2] = (comm != null ? new EmailAddress(comm.Value) : null);

            // Specify two IM addresses.

            // Specify the home address.
            var address = contact.AddressList.FirstOrDefault(x => x.AddressType != null && x.AddressType.Id == Constants.AddressType.Private);
            if (address != null) {
                var paEntry = new PhysicalAddressEntry
                    {
                        Street = address.Street,
                        City = address.City,
                        State = address.Region,
                        PostalCode = address.PostalCode,
                        CountryOrRegion = address.CountryName
                    };
                exContact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry;
                if (contact.PostalAddress == address) {
                    exContact.PostalAddressIndex = PhysicalAddressIndex.Home;
                }
            } else {
                exContact.PhysicalAddresses[PhysicalAddressKey.Home] = null;
            }
            address = contact.AddressList.FirstOrDefault(x => x.AddressType != null && x.AddressType.Id == Constants.AddressType.Business);
            if (address != null) {
                var paEntry = new PhysicalAddressEntry
                    {
                        Street = address.Street,
                        City = address.City,
                        State = address.Region,
                        PostalCode = address.PostalCode,
                        CountryOrRegion = address.CountryName
                    };

                exContact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry;
                if(contact.PostalAddress == address) {
                    exContact.PostalAddressIndex = PhysicalAddressIndex.Business;
                }
            } else {
                exContact.PhysicalAddresses[PhysicalAddressKey.Business] = null;
            }

            // Save the contact.
            if (exContact.IsNew) {
                exContact.Save();
            } else {
                exContact.Update(ConflictResolutionMode.AlwaysOverwrite);
            }

            pathList.AddRange(this.AddFileAttachments(this.Access.IndependService.GetContact(exContact.Id.UniqueId), contact.GetDocuments()));
        } catch(Exception e) {
            log.Error("Error updating/inserting Contact in Exchange.", e);
        } finally {
            foreach (var path in pathList) {
                this.Access.Context.Container.Resolve<IDocumentService>().UndoCheckOut(path);
            }
        }
    }

当我执行此操作以进行更新时,我在行ErrorIncorrectUpdatePropertyCount

上收到错误代码exContact.Update(ConflictResolutionMode.AlwaysOverwrite);的异常

有人能帮帮我,问题是什么? - 谢谢。

1 个答案:

答案 0 :(得分:1)

解决方案是我必须检查,如果字符串值是空字符串,并且在这种情况下设置为null。通过这种方式,一切正常。