Reputation: 6011
I'm working on a C# Console Application that creates and/or updates Outlook Contacts for 11 users using the Exchange Web Services (EWS).
The application needs to impersonate these 11 users in order to properly create/update their respective Outlook Contacts.
The application has a dedicated [ServiceAccount]
who happens to be a member of the ApplicationImpersonation
admin role.
I'm launching the application and it runs properly.
To validate that everything worked, I navigate to Office 365 and sign into User1’s account (using his credentials) and I can see the 223 newly created Contacts.
I'm happy, I sign out.
The next day, the first thing I do is log into User1’s account (using his credentials) and I no longer see 223 Contacts but instead I see 7 Contacts.
For unknown reason, 216 Contacts have completely vanished from User1’s Contacts.
I look into User1’s Deleted Items and I see these 216 Contacts in there...
How does one even start to investigate this? I’m no expert with Exchange.
Here is the code I use to create the Contacts
private void AddNewOutlookContacts(IEnumerable<Person> contactsToCreate, string emailToImpersonify)
{
var service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new NetworkCredential(_serviceAccount, _serviceAccountPwd, _domain);
service.Url = new Uri(_office365ExchangeUrl);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailToImpersonify);
Collection<Contact> contactItems = new Collection<Contact>();
foreach (var c in contactsToCreate)
{
Contact newContact = new Contact(service);
newContact.GivenName = c.FirstName;
newContact.MiddleName = c.LastName;
newContact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;
newContact.DisplayName = string.Format("{0} {1}", c.FirstName, c.LastName);
newContact.PhoneNumbers[PhoneNumberKey.HomePhone] = c.HomePhone;
newContact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = c.WorkPhone;
newContact.PhoneNumbers[PhoneNumberKey.MobilePhone] = c.CellPhone;
newContact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress(c.Email);
newContact.JobTitle = c.JobTitle;
contactItems.Add(newContact);
}
// Batch Create...
ServiceResponseCollection<ServiceResponse> response = service.CreateItems(contactItems, WellKnownFolderName.Contacts, null, null);
if (response.OverallResult == ServiceResult.Success)
{
Console.WriteLine("SUCCESS");
}
else
{
Console.WriteLine("FAILED");
}
}
There is no code whatsoever that deletes Contacts.
Any ideas on what could be running on Exchange causing these Contacts to automagically disappear.
Is there a Log file I can look into ?
Anything that can help me shed some light on this would be great!
And one last thing, the disappearing of Contacts happens on all these 11 users.
Upvotes: 1
Views: 53
Reputation: 6011
I have found the culprit and here is the solution:
In the code I previously provided I omitted (for brevity reasons) the fact that I was inserting more than one email address like so:
if (!string.IsNullOrEmpty(c.RLEmail))
newContact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress(c.Email);
if (!string.IsNullOrEmpty(c.PersonalEmail))
newContact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress(c.PersonalEmail);
if (!string.IsNullOrEmpty(c.UnionEmail))
newContact.EmailAddresses[EmailAddressKey.EmailAddress3] = new EmailAddress(c.UnionEmail);
Although the code checks for IsNullOrEmpty()
before inserting an email address, what the code doesn’t do is verify the uniqueness of those email addresses.
This was the problem.
With my test data, I was creating a bunch of Contacts with a unique EmailAddress1
.
But for EmailAddress2
and EmailAddress3
, my test data did not have unique email addresses so duplicate addresses existed in those two fields.
For some reason, I was under the impression that these secondary and third email addresses didn’t matter if they had duplicates but I was wrong (and it makes perfect sense for them to be unique as well).
Long story short, Exchange must have a running job that cleans and removes Contacts that holds duplicate email addresses in EmailAddress2
and EmailAddress3
.
Upvotes: 1