How to apply multiple filters using Microsoft Graph API
Asked Answered
J

4

7

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.

Juba answered 6/5, 2021 at 13:49 Comment(5)
you are missing $ before filter - $filterGoodale
Thanks for the reply but with the beta version the $ is optional so it works with and withoutJuba
Yeah, i agree with Steve with beta version it's optional. BTW what exact error you're getting along with the requestid, timestamp when you make the call?Effluvium
Thanks - I get a http 400 response of BadRequest and message 'Filter not supported.'. Sorry for asking, but how will the requestId and timestamp help?Juba
As per a Microsoft Azure AD support engineer, this version currently in beta does not allow other filters with the signInActivity although if and when it is available in v1.0 (or a new version) they are assuming this functionality will be improved (I hope so)Juba
P
6

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.

Precursor answered 9/12, 2021 at 20:2 Comment(1)
You're missing the point of the question here, the original question states that a filter on signInActivity cannot be combined with other clausesOrdinarily
P
0

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.

Phalan answered 11/8, 2022 at 17:23 Comment(0)
E
0

replacing spaces by %20 is working for me:

$filter=UserType%20eq%20'Guest'and%20signInActivity/lastSignInDateTime%20le%202021-04-26T17:30:09Z
Evidentiary answered 8/11, 2022 at 8:28 Comment(0)
U
-3

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

Umbles answered 28/7, 2021 at 12:11 Comment(1)
Thanks, I got excited by this but just tried with your example and I get the following error :- "Query option '$filter/filter' was specified more than once, but it must be specified at most once."Juba

© 2022 - 2024 — McMap. All rights reserved.