如何使用 Azure AD Graph API 查询所有直接下属?

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

我一直在使用以下方法查询经理的所有直接下属:

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
}

我刚刚发现这些结果是不完整的。我看到一个经理有31个直接下属的情况,但我的代码只有returns 19个。此外,DirectReports.MorePagesAvailablefalse

如果我单独查询未包含在直接报告集合中的其他用户并展开其经理,我可以看到该经理是预期的经理,因此 Azure AD Graph 中存在正确的关系。

然后,我尝试查询所有我关心的manager的用户,但是下面的代码是无效的:

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

我收到以下错误:

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

使用Microsoft.Azure.ActiveDirectory.GraphClient查询一个用户的所有直接下属的正确方法是什么?

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

根据代码,您从 ObjectId 等于 objectId 的用户处检索了直接下属。请确保用户是您提到的经理。

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

Azure AD Graph同样使用分页功能。像上面的代码一样,我们应该检查 MorePagesAvailable 属性 并使用 GetNextPageAsync().

获取下一页

这里是一段代码,通过分页结果打印来自经理的直接报告。您可以将 managerId 替换为该经理的 objectId:

    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);
            }
        }
    }