Home > Back-end >  How to convert IGroupMembersCollectionWithReferencesPage from Azure AD to list/collection in c#?
How to convert IGroupMembersCollectionWithReferencesPage from Azure AD to list/collection in c#?

Time:07-20

I want to convert an object to a list so i can iterate over the items, how can this be done? The members variable returns a IGroupMembersCollectionWithReferencesPage. How can i convert this to a list?

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options); var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        try
        {
           // convert members to a list or collection
            var members = await graphClient.Groups["group-id"].Members.Request().GetAsync();

            return new JsonResult(members);
        }
        catch (Exception ex)
        {
            Logger.Warning(ex.Message);
            throw;
        }

CodePudding user response:

To convert IGroupMembersCollectionWithReferencesPage from Azure AD to list, please try the below script mentioned in this SO Thread by user2250152 like below:

List<User> users = new List<User>();
var groupMembers = await Graph.Groups[groupId].Members.Request().GetAsync();
users.AddRange(groupMembers.CurrentPage.OfType<User>());
while(groupMembers.NextPageRequest!=null)
{
    groupMembers = await groupMembers.NextPageRequest.GetAsync();
    users.AddRange(groupMembers.CurrentPage.OfType<User>());
}

Alternatively, you can try using the below script by Tony Ju in this SO Thread:

IGroupMembersCollectionWithReferencesPage lstOfAdusers =  graphServiceClient.Groups["groupID"].Members.Request().GetAsync().Result;

var memberoflist=graphServiceClient.Users["userID"].MemberOf.Request().GetAsync().Result;

Please check the below reference if it gives you a pointer:

C# Azure AD Graph get all members of a group where are more than 20 by Tom Sun - MSFT

  • Related