Adding a SP.FieldUserValue into the Visitor/Member/Owner group of a site
-
11-01-2021 - |
Вопрос
I'm creating a website based on what a user submits in a form
3 fields are reserved for the 3 types of default groups that you're asked for when creating a subsite manually :
- Owner
- Members
- Visitors
I noticed that these groups aren't created when creating the site via JavaScript
So I created them manually, after retrieving the field's value containing the users(requestData
is a list line and value
is the field internalName) :
SetupGroup(value, suffix)
{
var users = this.requestData.get_item(value);
var groupInfo = new SP.GroupCreationInformation();
groupInfo.set_title(this.subweb.get_title() + ' - ' + suffix);
groupInfo.set_description('Ca marche presque correctement !');
var newGroup = this.subweb.get_siteGroups().add(groupInfo);
users.forEach((user)=>{
newGroup.get_users().add(user);
});
return newGroup;
}
But the content of the field is SP.FieldUserValue
instead of SP.User
and i get the error The parameter loginName cannot be empty or bigger than 251 characters
once i try to throw a query to save this
How can I achieve it ?
Also, when the group is created, do i have to simply replace the Associated groups as such, or do I have to give the permissions to each groups myself to get the same behavior as a manually created site :
this.subweb.set_associatedVisitorGroup(this.SetupGroup('Visiteurs_x0020__x0028_Lecture_x'), 'Membres');
(SetupGroup
returns a group)
Решение
Finally found out
Here's the code, users
is a collection of SP.FieldUserValue
:
AddUsersToGroup(users, group)
{
users.forEach((user, iteration)=>{
var userID = user.get_lookupId();
var userInfo = this.web.get_siteUserInfoList().getItemById(userID);
this.context.load(userInfo);
this.context.executeQueryAsync(()=>{
var newUser = this.web.ensureUser(userInfo.get_item('Name'));
group.get_users().addUser(newUser);
this.context.load(group);
this.context.executeQueryAsync(()=>{
console.log('User added : ' + userInfo.get_item('Title'));
}, QueryError);
}, QueryError);
});
}
This will just add the user list to a given group, now if you want to give these groups the same rights as the Visitor/Member/Owner group, use the following guide : https://msdn.microsoft.com/en-us/library/office/hh185014%28v=office.14%29.aspx?f=255&MSPPError=-2147217396
And apply the roles :
SP.RoleType.administrator
for ownersSP.RoleType.contributor
for membersSP.RoleType.reader
for visitors