I am trying to add an external contact to global address list using EWS. So far I have failed to do so. Event newly added contact not visible in user primary contact list. Is there anything I am missing here? I have tried two version. One using EWS which is follow:
var service = InitConnection(exchangeUrl, exchangeUsername);
if (service == null)
{
return "Failed to connect to exchange server";
}
try
{
var data = (Hashtable)contactKeyValues;
if (data == null && data.Count <= 0)
{
return "The data can't be extracted. Please check your dictionary parameter of properties.";
}
var localExchangeContactObj = ValidateAndBuildContact(data);
var remoteExchangeContactObj = new Contact(service);
remoteExchangeContactObj.GivenName = localExchangeContactObj.FirstName;
remoteExchangeContactObj.Surname = localExchangeContactObj.LastName;
remoteExchangeContactObj.Initials = localExchangeContactObj.Initials;
remoteExchangeContactObj.FileAsMapping = FileAsMapping.GivenNameSpaceSurname;
remoteExchangeContactObj.PhoneNumbers[PhoneNumberKey.BusinessPhone] = localExchangeContactObj.PhoneBusiness;
remoteExchangeContactObj.PhoneNumbers[PhoneNumberKey.HomePhone] = localExchangeContactObj.PhoneHome;
remoteExchangeContactObj.EmailAddresses[EmailAddressKey.EmailAddress1] = localExchangeContactObj.Email;
var address = new PhysicalAddressEntry();
address.Street = localExchangeContactObj.Address;
address.City = localExchangeContactObj.City;
address.State = localExchangeContactObj.State;
address.PostalCode = localExchangeContactObj.Zip;
address.CountryOrRegion = localExchangeContactObj.Country;
remoteExchangeContactObj.PhysicalAddresses[PhysicalAddressKey.Business] = address;
remoteExchangeContactObj.Body = new MessageBody(BodyType.HTML, localExchangeContactObj.Notes);
remoteExchangeContactObj.Save(WellKnownFolderName.Contacts);
return remoteExchangeContactObj.Id.UniqueId.ToString();
}
catch (Exception exception)
{
return string.Format("Something went wrong while creating contact in exchnage. Here is the details {0}", exception.ToString());
}
The other approach is through LDAP, in which case it creates the contact in current OU, but dont show up while searching. Code as below:
try
{
DirectoryEntry de;
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
{
de = new DirectoryEntry(ldapPath);
}
else
{
de = new DirectoryEntry(ldapPath, username, password);
}
var newContact = de.Children.Add("CN=" + name, "contact");
newContact.Properties["mail"].Value = email;
de.CommitChanges();
}
catch (COMException comEx)
{
var eventLog = new EventLog { Source = "Exchange Integration Create Contact" };
eventLog.WriteEntry(string.Format("Exception while creating contact {0} ", comEx), EventLogEntryType.Error);
return comEx.ErrorCode.ToString();
}
catch (Exception exception)
{
var eventLog = new EventLog { Source = "Exchange Integration Create Contact" };
eventLog.WriteEntry(string.Format("Exception while creating contact {0} ", exception), EventLogEntryType.Error);
return exception.ToString();
}
return "Contact created";
You can't create contacts in the GAL using EWS, EWS is a Mailbox Access API so you can create contacts in a Mailbox's contact folders which is what your code is trying to do. However these will not appear in the Global Address List. The only supported way to create contacts that will appear in the GAL is to use the Exchange Management Shell New-MailConact https://technet.microsoft.com/en-us/library/bb124519(v=exchg.150).aspx cmdlet and use Remote Powershell in your Managed code with https://msdn.microsoft.com/en-us/library/office/ff326159(v=exchg.150).aspx . This will create a Mail-enabled AD Contact. (Your LDAP code is creating a contact but it won't be mail enabled if you really want to go down the route you need to set a lot more properties such as TargetAddress etc look at existing Mail Enabled contact)