Search code examples
c#.netexchange-serverexchangewebservices

Why can't I access the HasPicture property?


I'm trying to manage contact objects with a C# application.

I'm loading the contacts with BindToItems:

ServiceResponseCollection<GetItemResponse> responses = service.BindToItems(itemIds, PropertySet.FirstClassProperties);
foreach (var responseItem in responses)
{
    contactDict.Add(responseItem.Item.Id, (Contact)responseItem.Item);
}

Then I try to check if the contact has changed by comparing its properties with my stored values.

         return exchangeContact.Surname != user.Surname
             || exchangeContact.CompanyName != user.Company
             ...
             || (!exchangeContact.HasPicture && user.ThumbnailPhoto != null)

But when I try to access the HasPicture property, an exception is thrown.

ServiceObjectPropertyException: This property was requested, but it wasn't returned by the server.

Checking in the debugger, this exception is also thrown for other properties of the Microsoft.Exchange.WebServices.Data.Contact. Examples are:

  • Birthday
  • ContactSource
  • IconIndex
  • NormalizedBody
  • TextBody
  • PostalAddressIndex
  • WeddingAnniversary

Why can't I access those properties? Are they not included in the FirstClassProperties? But this article says that HasImage is a FirstClassProperty of Contact objects.


Solution

  • Following on from BastianW comment, you can check if the account has a picture by using the Contact.TryGetProperty method.

    bool HasPicture;
    exchangeContact.TryGetProperty(ContactSchema.HasPicture, out HasPicture);