Home > Enterprise >  Department not null filter for IGraphServiceUsersCollectionRequest (GraphServiceClient)
Department not null filter for IGraphServiceUsersCollectionRequest (GraphServiceClient)

Time:03-24

How to create not null filter for department property to get users by GraphServiceClient?

department ne null

and

NOT(department eq null)

don't work

My current code:

var request = _graphClient.Users.Request()
    .Filter($"department ne null");
        
var usersPage = await request.GetAsync();

CodePudding user response:

Filtering by department requires adding header ConsistencyLevel:eventual and $count parameter must be set to true.

According to the documentation department is returned only on $select. It means that if you need to know the value of department you have to add Select method and specify all properties to be returned including department.

var queryOptions = new List<QueryOption>()
{
    new HeaderOption("ConsistencyLevel", "eventual");
    new QueryOption("$count", "true")
};

var request = await _graphClient.Users
    .Request( queryOptions )
    .Select("id,displayName,department") // and other properties
    .Filter("department ne null"); // .Filter("NOT(department eq null)") should also work

var usersPage = await request.GetAsync();

Resources:

Advanced queries

User properties

  • Related