error Message: Unsupported or invalid query filter clause specified for property 'userType' of resource 'User'. Microsoft Graph SDK Filter()

3.5k views Asked by At

I am Microsoft Graph API/SDK to retrieve users from Azure Active Directory. My Filter() function giving me error Message: Unsupported or invalid query filter clause specified for property 'userType' of resource 'User'.

My code is:

var azureUsers = await graphClient
              .Users
              .Request().Filter("startsWith(userType, 'P')")
              .Select(x => new
              {
                x.Id,
                x.UserType,
                x.DisplayName,
                x.GivenName,
                x.Surname,
                x.UserPrincipalName,
                x.AccountEnabled,
                x.Identities,
                x.BusinessPhones,
                x.JobTitle,
                x.MobilePhone,
                x.OfficeLocation,
                x.PreferredLanguage,
                x.Mail,
                x.Extensions,
                x.CreatedDateTime
              })
              .GetAsync();

It works absolutely fine with

.Filter("startsWith(mail, 'P')")

But not with the user type.

I have this attribute UserType in my Azure AD

Our guest users are around 700 and I want to exclude them in call.

It's giving me correct values with

        x.UserType,

But I want to filter this in Call. Any help would be much appreciated.

2

There are 2 answers

3
Harshita Singh On

You are seeing this error, because this filter is not supported for UserType attribute - as the error message explains.

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 check out Marc's answer on the same - How to filter users by userType = null?

0
Sruthi J On

You can use the below code to get the users list without guest user and according to document usertype should member or guest.please refer to the document

enter image description here

 public static async Task ListUsers(GraphServiceClient graphClient)
            {
                Console.WriteLine("Getting list of users...");
                // Get all users (one page)
                var result = await graphClient.Users
                    .Request()
                    .Select(e => new
                    {
                        e.DisplayName,
                        e.Id,
                        e.Identities
    
    
                    }).Filter($"usertype eq 'Member'")
                    .GetAsync();
    
                foreach (var user in result.CurrentPage)
                {
                    var directoryObject = await graphClient.DirectoryObjects[user.Id].Request()
                         
     .GetAsync();
                    Console.WriteLine(JsonConvert.SerializeObject(directoryObject));
                }
    
            }