简体   繁体   中英

Microsoft Graph: Get Children of all Child Groups (.NET)

I'm using Graph API in a .NET Blazor project to get all members of a group. It works great unless the group has other groups as members and not just users. The code I'm using is as follows:

var groups = await GraphServiceClient.Groups.Request()
    .Filter($"mail+eq+'{email}'")
    .Expand("members")
    .GetAsync(); //get the member groups by email, then include member information
var members = groups.FirstOrDefault().Members.ToList(); //get the first group (there should be only one) and convert it to list
return members.Select(s => ((User)s).DisplayName).ToList(); //cast the items to User and get the display name, then return this list

It fails when I try to cast a Group to a User. Of course I could filter the Groups out using .Where , but then I would miss the users they contain.

How can I get the members of all child groups (as many levels as they exist) and return them all at once?

What you want to do is called "recursion," which should be your default search term whenever you're working with nested objects. You can write a recursive function of your own, or try using Linq recursion. They are a little tricky to get a handle on, but ultimately very useful.

However, it seems that the API already includes a function called "transitiveMembers" that might be what you're looking for:

Microsoft Documentation

Note: I don't know the return format of that API call, but it looks like it will have to be filtered, as it will include nested group objects, devices and so on. I'd also like to say that in this context, I believe "members" to be something like class members, not the actual user members of each group.

do you mind sharing the solution please_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM