How to apply multiple filters using Microsoft Graph API

Viewed 6077

I am new to using the Graph API and trying to use the beta version to get user information and want to filter based on userType and signInActivity/lastSignInDateTime. Individually the filters work fine on their own but together they error with a "filter not supported". This happens in the Graph Explorer also but the documentation indicates I should be able to filter on more than one thing? I can only think if it is a restriction of the signInActivity filter specifically that doesn't allow you to do another filter?

These both individually work:

https://graph.microsoft.com/beta/users?filter=userType eq 'Guest'&select=displayName,id

https://graph.microsoft.com/beta/users?filter=signInActivity/lastSignInDateTime ge 2021-05-01T00:00:00Z&select=displayName,id

However the following does not work and returns a "Filter not supported" error:

https://graph.microsoft.com/beta/users?filter=signInActivity/lastSignInDateTime ge 2021-05-01T00:00:00Z and userType eq 'Guest'&select=displayName,id

Any help would be appreciated!

I see a similar question was asked here Microsoft Graph - Error when using multiple filters for CalendarView but no answer currently.

3 Answers

I found the answer to this -- basically, to add multiple clauses to your $filter, simply add the word "and" to separate each criteria. this is the example I used and it seemed to work quite well:

https://graph.microsoft.com/beta/users?$count=true&$filter=endsWith(userPrincipalName,'mydomain.com') and accountEnabled eq false&$orderBy=userPrincipalName&$select=id,displayName,userPrincipalName,department,employeeid,jobtitle,accountEnabled

as it turns out, odata seems to not worry too much about spaces.

Those 2 filter types cannot be combined as of today.

I suggest pulling the data based on login and then reduce the list of last login down to Guests

- set_fact:  
    foo: "{{ bar | selectattr('userType', '==', 'Guest') | list }}"

On a sidenote: Thank you @soler - your answer helped me realize where I was screwing up a different filter statement.

Found this post as I had the same issue.. adding the filter statement twice worked for me https://graph.microsoft.com/beta/users?$count=true&$filter=UserType eq 'Guest'&$filter=signInActivity/lastSignInDateTime le 2021-04-26T17:30:09Z&$select=displayName,UserType,signInActivity

Related