I'm using the "Create a modern team site" using the PnP/PnPjs, and I want to add a user to the "Members" group of the newly created site.

I tried using this code:

              let memberGroup = siteTitle + " " + "Members"
              console.log("Trying to get group: " + memberGroup)
              console.log("Adding user: " +  expLoginName)
              let web = new Web(targetSiteUrl);
              let group = web.siteGroups.getByName(memberGroup)

              group.users.add(expLoginName).then(function (d) {
                d.select("Id").get().then(userData => {

But I get an error message saying that it can't find the group. Is the group not available before a couple of minutes after the site is created? or am I doing something wrong?

enter image description here

UPDATE: GetById(5) worked, might change to GetByName() later if I figure out the correct way

5 Answers 5


You can use the associatedMemberGroup property. When you use it, you dont need to worry about the ID of the group or the group name. It will pick the default member group of the site collection and then you can add the users to it.

Sample code:

sp.web.associatedMemberGroup.users.add(expLoginName).then(d => {

I think you are on right path, they way I understood you want users to add to Default Members group created. First thing is Your group name won't be 'Members'. It would be your Site tile and then Members. Did you double check this ?

For e.g. site name is 'Simba' Hre, your default members group name would be 'Simba Members'

Then you can use

$pnp.sp.web.siteGroups.getByName("Simba Members").users
  • Thank you for the response. This is correct, the default members group yes. I just updated my post.
    – Dogemaster
    Jul 16, 2019 at 8:09
  • Hi...can you post screen shot of your groups which are available in site? Jul 16, 2019 at 8:55

You should not assume that Members group ID will always be 5. You can check the group ID manually from Site settings--> Site permissions--> open the required group--> check for MembershipGroupId=<group id> query string in URL or using any script(JS/PowerShell).

If you are not sure about group ID then you can get the group by using its name. Default group names in SharePoint will be in below format:

1. <Site Title><space>Members
2. <Site Title><space>Owners
3. <Site Title><space>Visitors

In below article you can find the different ways to get the SharePoint Group using PnP JS:

PnP-JS-Core: Get SharePoint Group.

Now coming to the below point:

I'm also a bit unsure on the loginName format.

The format you have mentioned is systemUserKey which you can get from _spPageContextInfo object on SharePoint pages.

Check your format is right or not using:


_spPageContextInfo object also provides the userLoginName property. So, I will suggest you to try using these property and see which one works for you.

_spPageContextInfo object provides so many properties. mentioning some of these below which maybe useful for you:

1. _spPageContextInfo.systemUserKey
2. _spPageContextInfo.userLoginName
3. _spPageContextInfo.userEmail
4. _spPageContextInfo.userPrincipalName
5. _spPageContextInfo.userId

Tested in a SPFX web part with the code snippet below:

    import pnp, {Web} from 'sp-pnp-js';

      public onInit(): Promise<void> {  
    // setup to use spfx context  
    return super.onInit().then(_ => {  
        spfxContext: this.context  

  public render(): void {
    pnp.sp.web.siteGroups.getByName("Dev Members").users

SiteGroup name can be checked with this Rest API:


enter image description here

And should pass user login name in add function, the normal format should be:


I checked the site settings, and the permissions for all the sites created:


and it seems like the OOTB Members group id is 5? Anyways I tried getbyid with 5.

          let group = web.siteGroups.getById(5)
          group.users.add(expLoginName).then(function (d) {
            d.select("Id").get().then(userData => {

and it worked, adding the user to the members group. But I will change it to getbyname later if I figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.