locked
Exchange Web Services Managed API Contact Photo RRS feed

  • Question

  • I am working with the Exchange Web Services Managed API and am having an issue with getting a contacts photo.  I am using FindItemsResult object to retrieve a collection of items with the following code:

            Dim contactsFolder As ContactsFolder = contactsFolder.Bind(EWS.EWService, parentFolder.FolderId, New PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount))
            Dim numItems As Integer = contactsFolder.TotalCount
    
            'Get all existing contacts in folder
            Dim view As ItemView = New ItemView(numItems)
            view.PropertySet = New PropertySet(BasePropertySet.FirstClassProperties, ContactSchema.Photo)
            Dim contacts As FindItemsResults(Of Item) = EWS.EWService.FindItems(parentFolder.FolderId, view)
            For Each i As Item In contacts
                If TypeOf i Is Contact Then
    
                    Dim p As Byte() = DirectCast(i, Contact).DirectoryPhoto
    
                End If
            Next

    The contact item always returns nothing for the DirectoryPhoto property even though the contact in exchange has a photo and the contact "HasPicture" property is true.

    Any ideas or examples of reading a contacts photo would be appreciated.

    Monday, June 17, 2013 2:55 PM

Answers

  • Hello Builder 66,

    I was expecting the same thing as you. I thought the EWS Managed API abstracted more of the process for getting contact photos. Here's code that will get you contact photos:

    static void GetContactPhoto(ExchangeService service) { ContactsFolder folder = Folder.Bind(service, WellKnownFolderName.Contacts) as ContactsFolder; PropertySet props = new PropertySet(ContactSchema.Photo, ContactSchema.HasPicture, ContactSchema.DisplayName); ItemView view = new ItemView(200); view.PropertySet = props; FindItemsResults<Item> results = folder.FindItems(view); IEnumerable<Contact> contacts = results.Cast<Contact>(); bool hasPic; foreach (Contact contact in contacts) { if (contact.TryGetProperty(ContactSchema.HasPicture, out hasPic)) { contact.Load(new PropertySet(ContactSchema.Attachments, ContactSchema.Photo)); // ContactSchema.Photo does nothing. foreach (Attachment attachment in contact.Attachments) { if ((attachment as FileAttachment).IsContactPhoto) { attachment.Load(); } }

    // Only works once the attachment is loaded. This does not load attachment. FileAttachment photo = contact.GetContactPictureAttachment(); } } }


      


    Michael | Microsoft Exchange Developer Content

    The Exchange Development Forum Guide has useful information for using the Exchange Development Forum.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.



    Tuesday, June 18, 2013 7:40 PM

All replies

  • Hello Builder 66,

    Perform a Contact.Load(New PropertySet(BasePropertySet.FirstClassProperties, ContactSchema.Photo). FindItems doesn't return a contact photo.

    With regards,


    Michael | Microsoft Exchange Developer Content

    The Exchange Development Forum Guide has useful information for using the Exchange Development Forum.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Tuesday, June 18, 2013 12:36 AM
  • Hi Michael,

    I gave that a try and am getting the same results.  One thing that seems inconsistence is there is no property called "Photo" in the Contact object, just a "DirectoryPhoto" property.  But the ContactSchema uses the .Photo value.  Not sure if this means anything.

    Tuesday, June 18, 2013 5:44 PM
  • Hello Builder 66,

    I was expecting the same thing as you. I thought the EWS Managed API abstracted more of the process for getting contact photos. Here's code that will get you contact photos:

    static void GetContactPhoto(ExchangeService service) { ContactsFolder folder = Folder.Bind(service, WellKnownFolderName.Contacts) as ContactsFolder; PropertySet props = new PropertySet(ContactSchema.Photo, ContactSchema.HasPicture, ContactSchema.DisplayName); ItemView view = new ItemView(200); view.PropertySet = props; FindItemsResults<Item> results = folder.FindItems(view); IEnumerable<Contact> contacts = results.Cast<Contact>(); bool hasPic; foreach (Contact contact in contacts) { if (contact.TryGetProperty(ContactSchema.HasPicture, out hasPic)) { contact.Load(new PropertySet(ContactSchema.Attachments, ContactSchema.Photo)); // ContactSchema.Photo does nothing. foreach (Attachment attachment in contact.Attachments) { if ((attachment as FileAttachment).IsContactPhoto) { attachment.Load(); } }

    // Only works once the attachment is loaded. This does not load attachment. FileAttachment photo = contact.GetContactPictureAttachment(); } } }


      


    Michael | Microsoft Exchange Developer Content

    The Exchange Development Forum Guide has useful information for using the Exchange Development Forum.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.



    Tuesday, June 18, 2013 7:40 PM
  • Thanks Michael,  that did the trick.

    Tuesday, June 18, 2013 8:23 PM
  • Hi,

    I was trying but could able to get the records from EWS API. Please suggest.

              

     ContactsFolder folder = Folder.Bind(service, WellKnownFolderName.Contacts) as ContactsFolder;



                PropertySet props = new PropertySet(BasePropertySet.FirstClassProperties,ContactSchema.Photo
                                                    //ContactSchema.HasPicture,
                                                   // ContactSchema.DisplayName
                                                   // ContactSchema.HasPicture
                                                    );

                ItemView view = new ItemView(200);
                view.PropertySet = props;

                FindItemsResults<Item> results = folder.FindItems(view);

    Here count of results is always giving Zero. 

    Thanks in Advance.

    Regards,

    Guruprasad

    Friday, February 2, 2018 4:28 PM