kiewic
kiewic

Reputation: 16450

How to query all direct reports using Azure AD Graph API?

I've been querying all the direct reports of a manager using:

var users = activeDirectoryClient.Users
    .Expand(x => x.DirectReports)
    .Where(d => d.ObjectId == objectId);

var foundUsers = users.ExecuteAsync().Result;
foreach (var foundUser in foundUsers.CurrentPage)
{
    IUser user = foundUser as User;
    int directReportsCount = user.DirectReports.CurrentPage.Count; // 19

    // ...

    Console.WriteLine(user.DirectReports.MorePagesAvailable); // false
}

I just discovered that these results are incomplete. I see a case where a Manager has 31 direct reports, but my code only returns 19. Moreover, DirectReports.MorePagesAvailable is false.

If I query individually for the other users not included in the direct reports collection and I expand its manager, I can see the manager is the expected one, so the right relationship exists in the Azure AD Graph.

Then, I tried to query all users with the manager I care, but the following code is invalid:

var users = activeDirectoryClient.Users
    .Where(d => d.Manager.ObjectId == objectId);

I get the following error:

System.ArgumentNullException: 'Value cannot be null. Parameter name: key'

What is the right way to query all the direct reports of a user using Microsoft.Azure.ActiveDirectory.GraphClient?

Upvotes: 0

Views: 2176

Answers (1)

Fei Xue
Fei Xue

Reputation: 14649

I see a case where a Manager has 31 direct reports, but my code only returns 19.

Based on the code, you were retrieve the direct reports from the user whose ObjectId equals objectId. Please ensure that the user is that manager you mentioned.

What is the right way to query all the direct reports of a user using Microsoft.Azure.ActiveDirectory.GraphClient?

It is same for Azure AD Graph to use the paging feature. Like the code above, we should check MorePagesAvailable property and get the next pages with GetNextPageAsync().

And here is a piece of code that print the direct reports from the manager by paging the result. You can replace the managerId with the objectId of that manager:

    public void PrintDirectReports()
    {
        String managerId="";
        int pageSize=2;
        ActiveDirectoryClient client = GraphHelper.CreateGraphClient();
        int pageIndex = 1;
        var directoryRecports = client.Users[managerId].DirectReports.Take(pageSize).ExecuteAsync().Result;
        Console.WriteLine($"Page{pageIndex++}:");
        foreach (var report in directoryRecports.CurrentPage)
        {
            Console.WriteLine(report.ObjectId);
        }

        while (directoryRecports.MorePagesAvailable)
        {
            Console.WriteLine($"Page{pageIndex++}:");
            directoryRecports = directoryRecports.GetNextPageAsync().Result;
            foreach (var report in directoryRecports.CurrentPage)
            {
                Console.WriteLine(report.ObjectId);
            }
        }
    }

Upvotes: 1

Related Questions