Search code examples
microsoft-graph-apioffice365exchangewebservicesmicrosoft-graph-mailmicrosoft-graph-contacts

How to filter/Search based on Schema extensions for contact resource in Microsoft Graph Rest api


I'm working on a application which suppose to be syncing contacts between local database to Ms Exchange using Ms graph Api.

I've to extend contact resource by using Schema Extensions provided by Microsoft, although I'm able to create Schema Extensions and also able to attach them with required contacts in order to extend them. But when I attempt to search on these schema Extension it's not working for me.

Here it is mentioned that Schema Extensions are available for contacts and support filtering and searching.

https://learn.microsoft.com/en-us/graph/extensibility-overview?tabs=csharp#choose-an-extension-type-for-your-application

https://learn.microsoft.com/en-us/graph/extensibility-overview?tabs=csharp#choose-an-extension-type-for-your-application

This is how I'm creating Schema Extension.

 var graphServiceClient = await _graphService.GetGraphServiceClientAsync();

            var requestBody = new SchemaExtension
            {
                Id = "SyncContactsExt",
                Description = "Schema Extension",
                TargetTypes = new List<string>
                    {
                        "Contact",
                    },
                Properties = new List<ExtensionSchemaProperty>
                    {
                        new ExtensionSchemaProperty
                        {
                            Name = "CVSID",
                            Type = "String",
                        },
                        new ExtensionSchemaProperty
                        {
                            Name = "SYNCH",
                            Type = "String",
                        },
                          new ExtensionSchemaProperty
                        {
                            Name = "CHET",
                            Type = "String",
                        }

                    },
            };
            
            
var results = await graphServiceClient.SchemaExtensions.PostAsync(requestBody);

Above code work okay.

Here is how I'm trying to associate these schema extensions with contact.

 var contact = await graphServiceClient.Users[userId].Contacts[contactId].GetAsync();
            
            contact.AdditionalData = new Dictionary<string, object>
            {
                {
                    "extnxuzruwv_SyncContactsExt",new
                        {
                            CVSID=schemaValue.CVSID,
                            SYNCH=schemaValue.SYNCH,
                            CHET=schemaValue.CHET
                        }
                }

            };


    contact = await graphServiceClient.Users[userId].Contacts[contactId].PatchAsync(contact);

Above code also works fine and I'm able to see values which I've updated for a contact using GetAsync() call.

Here is code snippet which I'm trying to filter contacts based on and it isn't working for me.

 var graphServiceClient = await _graphService.GetGraphServiceClientAsync();
            ContactCollectionResponse graphContacts=null;
            try
            {
                 graphContacts = await graphServiceClient.Users[id].Contacts.GetAsync((requestConfiguration) =>
                {
                  
                    requestConfiguration.QueryParameters.Filter = "extnxuzruwv_SyncContactsExt/CVSID eq '123'";
                    requestConfiguration.QueryParameters.Select = new string[] { "id", "givenName", "extnxuzruwv_SyncContactsExt" };
                    //requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");

                });

            }
            catch (Exception ex )
            {

            }

It gives me following error message.

Could not find a property named 'e2_266364b1a059462d9db999e09038e9d5_extnxuzruwv_SyncContactsExt' on type 'Microsoft.OutlookServices.Contact'."

Even I can see data I've populated to these custom fields/properties and those are available, and I can see them in contact response.


Solution

  • This is still listed as a restriction on the Schema Extensions for contacts on https://learn.microsoft.com/en-us/graph/known-issues#filtering-on-schema-extension-properties-not-supported-on-all-entity-types

    Filtering on schema extension properties is not supported on all entity types Filtering on schema extension properties (using the $filter expression) is not supported for Outlook entity types - contact, event, message, or post.

    The changelog doesn't indicate this has changed at all recently https://developer.microsoft.com/en-us/graph/changelog/?search=Schema%20extensions&filterBy=beta