bdcoder
bdcoder

Reputation: 3781

Microsoft.Graph.GraphServiceClient how to limit properties returned?

I am using Azure AD B2C. I was hoping to get a count of the number of users - however, according to the documentation:

Azure AD B2C currently does not support advanced query capabilities on directory objects. This means that there is no support for $count ...

Great, so I thought the next best thing to do was to perform a query to get all the ids of the users, i.e.:

var options = new TokenCredentialOptions
    {
       AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

var clientSecretCredential = new ClientSecretCredential( tenant_id, client_id, client_secret, options );

var scopes = new[] { "https://graph.microsoft.com/.default" };

var users = await graphClient.Users
                  .Request()
                  .Select( "id" )
                  .GetAsync();

// This shows other properties returned in addition to "id" ...

if ( users is not null )
{
   Console.WriteLine( JsonSerializer.Serialize( users ) );
}

I was under the impression (from the documentation) that using .Select( "id" ) would only return the "id" property, instead, the graph client returns what I assume are a default set of properties per user in addition to the "id", i.e.:

[
{"accountEnabled":null,
 "ageGroup":null, 
 ...more properties...,
 "id":"xxxx...",
 "@odata.type":"microsoft.graph.user"
}, 
{"accountEnabled":null,
 "ageGroup":null, 
 ... more properties ...,
 "id":"xxxx...",
 "@odata.type":"microsoft.graph.user"
}, ...
]

I am probably doing something wrong, but just how do you return ONLY the property (in this case "id") WITHOUT any other properties? I have yet to find an example or documentation that describes how to do this.

Thanks in advance.

Upvotes: 0

Views: 529

Answers (2)

bdcoder
bdcoder

Reputation: 3781

Thanks to @user2250152 and @Rukmini - the "answer" per se is that the graph QUERY returns only the selected / requested properties, that is, when the following code is used:

var users = await graphClient.Users
           .Request()
           .Select("id")
           .GetAsync();

The QUERY returns the "ids" as illustrated in @Rukmini's answer, but then the graphClient populates a list of "User" objects that is returned when the call completes. The User objects will contain the Id property as well as all the other properties associated with a User object as mentioned by @user2250152.

Thanks again for clearing this up.

Upvotes: 1

Rukmini
Rukmini

Reputation: 15574

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

To get the Azure AD b2c User IDs I used the below code:

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "b2corg.onmicrosoft.com";
var clientId = "ClientID";

var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var userName = "[email protected]";
var password = "password";

var userNamePasswordCredential = new UsernamePasswordCredential(
    userName, password, tenantId, clientId, options);

var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);
var users = await graphClient.Users
           .Request()
           .Select("id")
           .GetAsync();

users.ToList().ForEach(x => { Console.WriteLine("\nid: " + x.Id); });

enter image description here

I also tried in the Microsoft Graph Explorer, and I am able to fetch only the User IDs like below:

https://graph.microsoft.com/v1.0/users?$select=id

enter image description here

Upvotes: 0

Related Questions