1
votes

I created a small application that gets Contact objects from an external source. Then, depending on some configurations, I have to create/upadate these contacts in a user's contact folder on our exchange server, so that the next time this user opens its MS Outlook, he sees the new contacts (on the exchange server, I have a user with impersonation capabilities, so security is not a concern).

For that, I use the FindItems(folderId, filter, view) method of the EWS library that works good. For the filter, I'm using the user's email address which is quite a good key... If I get a result back, this simply means that the Contact already exist, and that I need to do an update instead of a create. Everything works as expected until here...

BUT, I encounter a problem when the Contact (email address in fact) is already existing in the GAL (Global Address List). In this case, the FindItems method returns no result even if the Contact exists in the folder! It seems (this is a supposition) that the exchange server creates a link for contacts which have an email address that already exist in the GAL and not a new contact. And this could explain why the FindItems method does not return anything in this case. The strange thing is that if I'm filtering on another property (for example on the combination of first and lastname), it works!

Currently, what happens is that for each Contact that already exist in the GAL, a creation instead of an update is done (because the FindItems method returns nothing), and as a result, the same Contact is created X time (instead of beeing created once, and then updated X-1 time).

The question is of course, how can I know if a Contact exists in an exchange folder when it already exists in the GAL?

Current code:

ItemView view = new ItemView(5)
{
   PropertySet = new PropertySet(BasePropertySet.FirstClassProperties)
};
SearchFilter f = new SearchFilter.IsEqualTo(ContactSchema.EmailAddress1, email);
FindItemsResults<Item> contactItems = _service.FindItems(folderId, f, view);
int resultCount = contactItems.Count(); // Always 0 if GAL, otherwise it works
1

1 Answers

0
votes

Finally, I solved my problem with an extended property by using the SetExtendedProperty method. In this extended field, I just put an Id and that solved the problem.

But that does not explain why the search is not working with an email address... If someone knows the answer, I'm still interested :)

The new search looks like this:

ItemView view = new ItemView(nb);
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, _extendedPropDef);
SearchFilter f = new SearchFilter.IsEqualTo(_extendedPropDef, contact.Id);
FindItemsResults<Item> contactItems = _service.FindItems(folderId, f, view);

With this code, everything works as expected...