Adding SP user to member group - Team Site
-
08-02-2021 - |
문제
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 => {
console.log(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?
UPDATE: GetById(5)
worked, might change to GetByName()
later if I figure out the correct way
해결책
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 => {
console.log(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
.add("i:0#.f|membership|email@domain.com").then(function(d){
console.log(d)
});
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.systemUserKey
_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(_ => {
pnp.setup({
spfxContext: this.context
});
});
}
public render(): void {
pnp.sp.web.siteGroups.getByName("Dev Members").users
.add("i:0#.f|membership|user@tenant.onmicrosoft.com").then(function(d){
console.log(d)
});
}
SiteGroup name can be checked with this Rest API:
https://tenant.sharepoint.com/sites/dev/_api/web/sitegroups/
And should pass user login name in add function, the normal format should be:
i:0#.f|membership|user@tenant.onmicrosoft.com
I checked the site settings, and the permissions for all the sites created:
https://tenant.sharepoint.com/sites/exp34/_layouts/15/userdisp.aspx?ID=7&Source=https%3A%2F%2Ftenant%2Esharepoint%2Ecom%2Fsites%2Fexp34%2F%5Flayouts%2F15%2Fpeople%2Easpx%3FMembershipGroupId%3D5
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 => {
console.log(userData);
});
});
and it worked, adding the user to the members group. But I will change it to getbyname
later if I figure it out.