Get MS Graph DirectReports count with ASP.NET SDK

Grozev, Dimitar 1 Reputation point
2021-12-06T09:15:19.893+00:00

Greetings,

I currently developing a feature which requires to find out a users direct reports count. Currently I get the direct reports through the GraphClient with the following code:

var directReports = await this.graphClient.Me.DirectReports.Request().GetAsync();

However this returns all of the information for these reports and I only need the count. How can this be achieved?

Thanks in advance!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,024 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,013 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 35,266 Reputation points
    2021-12-07T08:26:25.187+00:00

    Hi @Grozev, Dimitar

    If you just want to output the count, can try this:

    GET https://graph.microsoft.com/v1.0/me/directReports/$count  
    ConsistencyLevel: eventual  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Luca Spolidoro 1 Reputation point Microsoft Employee
    2021-12-14T13:57:16.383+00:00

    Hi @Grozev, Dimitar ,

    the .NET SDK is not updated yet to fully support the count on Directory Objects, but a preview should be coming next year.

    To workaround this limitation in the current version, you can use the following code:

    var requestUrl = graphClient.Me.DirectReports.AppendSegmentToRequestUrl("$count");  
    var options = new[] { new HeaderOption("ConsistencyLevel", "eventual") };  
    var request = new BaseRequest(requestUrl, graphClient, options);  
    var response = await request.SendRequestAsync(null, CancellationToken.None);  
    string count = await response.Content.ReadAsStringAsync();