Search code examples
c#graphazure-active-directorymicrosoft-graph-apimicrosoft-graph-sdks

Department not null filter for IGraphServiceUsersCollectionRequest (GraphServiceClient)


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();

Solution

  • 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