How to fetch M365 group members via pnp graph in SPFX

Kevin Thankachan 21 Reputation points
2021-02-11T00:10:03.923+00:00

I'm trying to list out all the members of a M365 group connected to a team site via a SPFX webpart.
Steps-

  1. get the Group id of the current team site.
  2. Using the group id, fetch the members of the group.

I don't see any function to list out the members of the group like (graph.groups.getById(groupId).Members??)

Here is the code snippet-

import { graph } from '@pnp/graph'  
import '@pnp/graph/Users' 
import '@pnp/graph/groups' 
//to get the group ID
 graph.groups.top(999).select('mailNickname,id').get().then(groups=>{

      groups.forEach(group=>{

        if(group['mailNickname']==currentSiteAlias)
        {
//set group ID
          groupId=group['id'];
//get the members
          graph.groups.getById(groupId).Members.then(memberObject=>{
            console.log(JSON.stringify(memberObject))
          }

          );

        }
      })
    }).catch((err) => {  
      console.log("Error fetching Group ID "+err)})
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,670 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,702 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2021-02-11T03:17:06.973+00:00

    Hi @Kevin Thankachan ,

    Use expand() like this:

     graph.groups.getById(groupId).expand("members")()  
    

    Here is the full code snippet, please refere:

      private GetGroupMembers()  
      {  
        graph.groups.top(999).select('mailNickname,id').get().then(groups=>{  
          console.log(groups);  
          groups.forEach( group=>{  
      
            if(group['mailNickname']== "NewProvision")  
            {  
               let groupId = group['id'];  
               const groupMembers =  graph.groups.getById(groupId).expand("members")().then(group=>{  
                console.log(group.members);  
              });  
      
            }  
          })  
        }).catch((err) => {  
          console.log("Error fetching Group ID "+err)});  
      
      }  
    

    Here is a similiar question here for your reference:

    PnPJS Graph : how to list all members of a Group

    Thanks
    Best Regards


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kevin Thankachan 21 Reputation points
    2021-02-11T06:14:41.337+00:00

    thanks a ton for the quick response @Jerryzy-MSFT

    0 comments No comments