Get count of users while reading from AAD [Microsoft Graph]

Viewed 70

I was following this documentation https://docs.microsoft.com/en-us/graph/aad-advanced-queries?tabs=csharp to run some graph query on AAD objects as follows:

await _graphServiceClient
         .Users.Request()
         .Request(new Option[] { new QueryOption("$count", "true") })
         .Header("ConsistencyLevel", "eventual")
         .Filter("endsWith(mail,'tenant.com')")
         .GetAsync();

I see the following error:

enter image description here

What am I missing and how do I resolve the same?

2 Answers
await _graphServiceClient
         .Users.Request(new Option[] { new QueryOption("$count", "true") })
         .Header("ConsistencyLevel", "eventual")
         .Filter("endsWith(mail,'tenant.com')")
         .GetAsync();

I tried to reproduce the same in my environment and got the below results:

To retrieve the list of users with Mail-ID, I executed the below query in Microsoft Graph Explorer:

GET https://graph.microsoft.com/v1.0/users?$count=true&$filter=endsWith(mail,'@tenant.com')

ConsistencyLevel:eventual

Response:

enter image description here

You can make use of the below sample CSharp code:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var queryOptions = new List<QueryOption>()
{
new QueryOption("$count", "true")
};
var users = await graphClient.Users
.Request( queryOptions )
.Header("ConsistencyLevel","eventual")
.Filter("endsWith(mail,'@tenant.com')")
.GetAsync();
Related