Search code examples
azure-active-directorymicrosoft-graph-sdksazure-ad-b2c

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.


Solution

  • 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.