RRR
RRR

Reputation: 131

How to retrieve Members under manager using Microsoft Graph using .NET MVC?

I am pretty new to C# and I'm trying to get List directReports under a manager who has logged in and authenticated with AAD. (GET /users/{id | userPrincipalName}/directReports) to get Alias name.

Thanks

ps. I am using ASP.NET MVC and this is the doc I am following

https://learn.microsoft.com/en-us/graph/api/user-list-directreports?view=graph-rest-1.0

Upvotes: 1

Views: 1030

Answers (2)

Jean-Marc Prieur
Jean-Marc Prieur

Reputation: 1659

You might want to see the reference documentation for directReports: https://learn.microsoft.com/en-us/graph/api/user-list-directreports?view=graph-rest-1.0

Also to get a token to acces the graph, you'll find the following sample (Web App calling Grap) very close to what you want to achieve: aspnetcore2-2-signInAndCallGraph

See HomeController.cs#L41-L64

public async Task<IActionResult> Contact()
{
 var scopes = new string[] { "user.read" };
 try
 {
  var accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes);
   CallGraphApiOnBehalfOfUser(accessToken);
   ...
  }
 }

Upvotes: 2

Brian Heinz
Brian Heinz

Reputation: 41

If you are struggling with how to make requests against the API, I would recommend using the GraphServiceClient nuget package, we use it and it is pretty good:

https://github.com/microsoftgraph/msgraph-sdk-dotnet

There are pretty good instructions in the README. You probably want something like this:

var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
    requestMessage
        .Headers
        .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

    return Task.FromResult(0);
}));

var directReports = await graphServiceClient.Users[userId].DirectReports.Request().GetAsync();
foreach (var directReport in directReports)
{
    //do something
}

Upvotes: 2

Related Questions