Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a filter query for Microsoft Graph that works equivalent to "contains"

I need to create a $filter query for the Microsoft Graph API that searches for a specific word in a string (the display name of users).

For example I'd want to be able to find all users that have "Esteban" in their names:

Luis Esteban
Alphonse Esteban
Esteban Luis
Alphonse Esteban Luis

The following query works, but only returns users that start with "Esteban" in their names, not users that contain "Esteban":

https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'Esteban')

I also tried using contains instead of startswith, but it gives an error response:

{ "error": { "code": "Request_BadRequest", "message": "An unknown function with name 'contains' was found. This may also be a key lookup on a navigation property, which is not allowed.", "innerError": { "request-id": "e5ed5c30-4e62-4497-8976-1d38167e759d", "date": "2018-09-13T23:17:17" } } }

And even though the Microsoft Graph docs say they support OData 4.0, they also say this:

"The contains string operator is currently not supported on any Microsoft Graph resources."

I also tried other commands given by the OData documentation and the construction rules that are supposed to be supported by filter and other query parameters.

In particular I tried combinations of these commands:

startswith
endswith
indexof
substring

but to no success. It seems the MS Graph API has no support for anything except startswith whatsoever.

I tried both v1.0 and the beta endpoints of the API.

Is there any other way, some smart combination of OData 4.0 commands and/or query parameters supported by the MS Graph API, that allows a search equivalent to contains?

PS: You can try out queries with the Graph Explorer here.

like image 611
Ricardo Guerrero Avatar asked Sep 13 '18 21:09

Ricardo Guerrero


People also ask

Is Microsoft Graph OData?

Final Notes. Microsoft Graph combined with OData is a very powerful technology that grants access to all members within an organization, it also prevents duplication of an organization members data and offers an option to reference these members by their organization id.

Is Microsoft Graph A graph database?

Graph databases everywhere: Microsoft Graph, Common Data Service, Cosmos DB, and Security Graph. Microsoft's interest in graph-based data is clear. CEO Satya Nadella described the Office 365 APIs, the foundation of what's now called the Microsoft Graph, as the company's “most important” bet.

Does the Microsoft-Graph API support $filter?

As per this blog post the microsoft-graph api only supports $filter with equals (eq) not equals (ne) greater than (gt) greater than or equals (ge) less than (lt), less than or equals (le) and (and) or (or) not (not) startswith any and $search is only supported for "messages” and “person” entities.

Does Microsoft Graph support the contains string operator?

And even though the Microsoft Graph docs say they support OData 4.0, they also say this: "The contains string operator is currently not supported on any Microsoft Graph resources." I also tried other commands given by the OData documentation and the construction rules that are supposed to be supported by filter and other query parameters.

Is it possible to do in-memory filter in graph API?

As you know, Graph API doesn't support your requirement now. My suggestion is to get the user list first and then do in-memory filter. This way also applicable to other no-supported Graph API. Meanwhile, make sure to vote up the existing feature request in User Voice or submit a new one.

Does the MS graph API support anything except startswith?

I also tried other commands given by the OData documentation and the construction rules that are supposed to be supported by filter and other query parameters. but to no success. It seems the MS Graph API has no support for anything except startswith whatsoever.


2 Answers

You can use the $search query parameter, but you must include the parameter ConsistencyLevel=eventual for it to work. Also, $top can have a maximum value of 999.

https://graph.microsoft.com/v1.0/users?$top=999&ConsistencyLevel=eventual&$search="displayName:Esteban"
like image 127
Dolly Talreja Avatar answered Sep 28 '22 02:09

Dolly Talreja


This appears to be the only alternative that will get you the list you want without filtering all users yourself

https://graph.microsoft.com/v1.0/me/people?$search=Esteban&$top=100000

As per this blog post the microsoft-graph api only supports $filter with
equals (eq)
not equals (ne)
greater than (gt)
greater than or equals (ge)
less than (lt), less than or equals (le)
and (and)
or (or)
not (not)
startswith
any

and $search is only supported for "messages” and “person” entities.

like image 43
codeye Avatar answered Sep 28 '22 02:09

codeye