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.

You are seeing this error, because this filter is not supported for
UserTypeattribute - as the error message explains.As a general rule of thumb,
userTypeshould be eitherMemberorGuest. The exception to this is when you're syncing an on-prem Active Directory. SinceuserTypeis 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
onPremisesSyncEnabledproperty. For synced users, this will be true, for cloud-native users it will be null.If you combine this with the
userTypeproperty, you can effectively retrieve only non-guest users using the following $filter:You can check out Marc's answer on the same - How to filter users by userType = null?