Albatross
Albatross

Reputation: 63

How can I get / set an extended property from a contact using Microsoft's EWS API?

I think I'm creating it properly, like as follows. c is a Contact, and I'm just trying to store a unique identifier considering that ItemId which is provided by EWS isnt static...

propertySetId = System.Guid.NewGuid();
// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "itemGUID", MapiPropertyType.String);
c.SetExtendedProperty(extendedPropertyDefinition, propertySetId.ToString());
c.Update(ConflictResolutionMode.AlwaysOverwrite);

When I try to pull this back out when searching for the contact based on something else, like first name, I'm getting a null returned. I'm trying to get the value by:

foreach (Item c in findResults.Items)
{
      foreach(ExtendedProperty extendedProperty in c.ExtendedProperties)
      {
            if(extendedProperty.PropertyDefinition.Name == "itemGUID")
            {
                  results[i] = extendedProperty.Value.ToString();
            }
      }
}

EDIT: code for findResults

List<SearchFilter> searchFilters = new List<SearchFilter>();
searchFilters.Add(new SearchFilter.IsEqualTo(itemGUID, value));
//can be more filters here depending on situation
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilters.ToArray());
findResults = service.FindItems(WellKnownFolderName.Contacts, filter, view);

Upvotes: 3

Views: 6762

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31651

You need to assign the PropertySet in the ItemView to tell EWS what properties to include when you search using FindItems. If you don't include it in your ItemView it won't be available for reading.The alternative approach is to use the Contact.Bind and request the property for each Contact in question (more service requests, but sometimes necessary).

See Viewing Extended Properties using EWS for a full example on working with Extended Properties in EWS.

Approach #1: Retrieve Extended Property for all Contacts

ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "itemGUID", MapiPropertyType.String);
ItemView view = new ItemView(50) { PropertySet = new PropertySet(propDef) };

Approach #2: Bind one contact at a time if you have a Contact ID

ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "itemGUID", MapiPropertyType.String);
Contact contact = Contact.Bind(service, contactID, new PropertySet(propDef));

Upvotes: 2

Related Questions