Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter users by userType = null?

I am trying to filter out Guest users in my Graph query. Since the ne comparison operator is not supported I was trying $filter=userType eq 'Member' or userType eq null instead. That fails too though. Any known workarounds to list users with null as userType?

Without that I need to download about a million rows each time and throw 4 out of 5 away on the client side, which is pretty slow and wasteful.

{
"error": {
    "code": "Request_UnsupportedQuery",
    "message": "Unsupported or invalid query filter clause specified for property 'userType' of resource 'User'.",
    "innerError": {
        "request-id": "411f7927-c3af-4042-a619-eee1c88971a0",
        "date": "2018-03-17T18:28:35"
    }
}
like image 821
Matthieu Maitre MSFT Avatar asked Sep 15 '25 21:09

Matthieu Maitre MSFT


2 Answers

As a general rule of thumb, userType should be either Member or Guest. The exception to this is when you're syncing an on-prem Active Directory. Since userType is an Azure AD property, the value for a synced user will be null.

If you can safely assume that your on-prem users are not guests, you can filter Azure AD user's based on if they're synced or cloud-native. You do this by looking at the onPremisesSyncEnabled property. For synced users, this will be true, for cloud-native users it will be null.

If you combine this with the userType property, you can effectively retrieve only non-guest users using the following $filter:

$filter=onPremisesSyncEnabled eq true OR userType eq 'Member'

You can also avoid this entirely if you Enable synchronization of UserType in Azure AD Connect.

like image 123
Marc LaFleur Avatar answered Sep 17 '25 11:09

Marc LaFleur


Update as of June 2021

the ne comparison operator is now supported on userType on graph! So you can now get users that are not guest users with this query: https://graph.microsoft.com/v1.0/users/?$filter=userType ne 'guest'&$count=true

edit: Important to note that this is an advanced query as so it requires the header ConsistencyLevel to be set to eventual and include the $count=true query parameter

here is a link to graph explorer with the query that you can run

like image 40
Ryan Weaver Avatar answered Sep 17 '25 10:09

Ryan Weaver