Search code examples
c#.netexchange-serverexchangewebservices

ExchangeService.FindItems()


I've been requested to create an application that should create a common base from 3 different email addresses sources and update each base taking the most common updated dataset. Among the three sources I have an Exchange server Contacts addressbook. I know that I can access such data trough EWS and in specific I should probably use the ExchangeService.FindPeople() method or maybe FindPersona(). This should work but, since I'm looking only for new/updated contacts, it will load significantly the server (maybe not for new records but I cannot understand how to retrieve update records) and this it's not a good practice. I've found in MSDN docs a way to be notified on updates on message base but there's nothin related on updates on contacts:

https://msdn.microsoft.com/en-us/library/office/dn458791(v=exchg.150).aspx

Is there something to be notified on updates on contacts (even trough third party products/APIs).

P.S. I would like to code in C# (or other .NET languages) but I'm open to anything else.


Solution

  • You should be able to check for newly created contacts by iterating through the contacts and accessing the DateTimeCreated property on each.

    To check for updated contacts, you could use the LastModifiedTime property.

    // Get the number of items in the Contacts folder.
    ContactsFolder contactsfolder = ContactsFolder.Bind(service, WellKnownFolderName.Contacts);
    
    // Set the number of items to the number of items in the Contacts folder or 50, whichever is smaller.
    int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;
    
    // Instantiate the item view with the number of items to retrieve from the Contacts folder.
    ItemView view = new ItemView(numItems);
    
    // To keep the request smaller, request only the DateTimeCreated and LastModifiedTime properties.
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DateTimeCreated, ContactSchema.LastModifiedTime);
    
    // Retrieve the items in the Contacts folder that have the properties that you selected.
    FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);
    
    // Display the list of contacts. 
    foreach (Item item in contactItems)
    {
        if (item is Contact)
        {
            Contact contact = item as Contact;
            if (contact.DateTimeCreated.Date == DateTime.Today.Date)
            {
               //Notify - Newly created contact
            }
    
            if (contact.LastModifiedTime.Date == DateTime.Today.Date)
            {
               //Notify - Newly modified contact
            }
        }
    }