Julien Turcotte
Julien Turcotte

Reputation: 65

Get all users with a specific email domain using Microsoft Graph SDKs

I would like to make this query to the Microsoft Graph API using the Microsoft Graph SDKs. I would like to get all users with the domain in email address is @something.com.

Use of $filter with the endsWith operator

GET ../users?$count=true&$filter=endsWith(mail,'@something.com')

I tried this following line of code :

var users= await _graphServiceClient.Users.Request().Filter("mail '@something.com'").Select(u => new {
                u.Mail,
                u.DisplayName,
            }).GetAsync();

The error I get is :

    Microsoft.Graph.ServiceException: 'Code: BadRequest
Message: Invalid filter clause

Without the filter, it works fine. Am I missing something?

Refs: Advanced query : https://learn.microsoft.com/en-us/graph/query-parameters Microsoft Graph SDKs : https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS

Upvotes: 2

Views: 1118

Answers (1)

user2250152
user2250152

Reputation: 20635

If you want to use $count query parameter you need to add the ConsistencyLevel header with eventual value.

GET /users?$count=true&$filter=endsWith(mail,'@something.com')
ConsistencyLevel: eventual

In C# specify header option and query option for the request:

var options = new List<Option>();
options.Add(new HeaderOption("ConsistencyLevel", "eventual"));
options.Add(new QueryOption("$count", "true"));

add endsWith operator to a filter.

var users = await _graphServiceClient.Users
    .Request(options)
    .Filter("endsWith(mail,'@something.com')")
    .GetAsync();

Upvotes: 3

Related Questions