Skip to content

Instantly share code, notes, and snippets.

@ainsofs
Created May 7, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ainsofs/003595b971da9d45fb26 to your computer and use it in GitHub Desktop.
Save ainsofs/003595b971da9d45fb26 to your computer and use it in GitHub Desktop.
Updating Title, Suffix, Gender, Email Addresses, Physical Address (Home, Business and Other) in Exchanged Managed API
//taken from: http://stackoverflow.com/questions/13475674/updating-a-contact-with-ews
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment