Microsoft.Graph获取组的所有用户

时间:2017-09-19 11:48:11

标签: c# microsoft-graph

我正在使用Microsoft.Graph,我想返回特定组中所有用户的列表。我找不到任何关于此的可消解信息。

我尝试过使用.Request().Expand(x => x.Members),但这只会返回20个用户。它们没有被分页(见here)。

我可以很好地提取群组列表,但是如何根据用户所在的群组过滤用户?这是我到目前为止所得到的......

public async Task Do()
{
    var graphClient = new GraphServiceClient(_authProvider);
    var groups = await graphClient.Groups.Request().GetAsync();

    do
    {
        foreach (var group in groups)
        {
            Console.WriteLine($"{group.Id}, {group.DisplayName}");
            Console.WriteLine("------");

            var filter = $""; // What goes here?
            var users = await graphClient.Users.Request().Filter(filter).GetAsync();

            do
            {
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id}, {user.GivenName} {user.Surname}, {user.MemberOf}");
                }
            }
            while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

            Console.WriteLine();
        }
    }
    while (groups.NextPageRequest != null && (groups = await groups.NextPageRequest.GetAsync()).Count > 0);
}

1 个答案:

答案 0 :(得分:5)

像这样的东西

public async Task Do()
{
    var graphClient = new GraphServiceClient(_authProvider);

    var groups = await graphClient.Groups.Request().GetAsync();

    do
    {
        foreach (var group in groups)
        {
            Console.WriteLine($"{group.Id}, {group.DisplayName}");
            Console.WriteLine("------");

            var users = await graphClient.Groups[group.Id].Members.Request().GetAsync();

            do
            {
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id}");
                }
            }
            while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

            Console.WriteLine("------");
            Console.WriteLine();
        }
    }
    while (groups.NextPageRequest != null && (groups = await groups.NextPageRequest.GetAsync()).Count > 0);
}