1

Seems simple right ?
Well, for the current user, it is, but when looking for a specific one :
https://www.google.fr/search?rlz=1C1GCEA_enFR779FR779&ei=ihWYWorCIoHxUpT0o7AO&q=sp+js+get+user+-current&oq=sp+js+get+user

Nothing.

I'm taking inputs from a listForm using JS, one of the fields requires the client to type in an existing username
When getting the value from the list I get a collection of SP.FieldUserValue wich only contains the ID and name of the user

Is there a way to add every user in this collection to a specific group ?

What I've tried so far :

  • Putting each field value directly in the group with myGroup.get_users().addUser() - "The LoginName cannot be null/empty or 256+ caracters"
  • Retreiving the User using myWeb.get_siteUserInfoList().getItemById(myField..get_lookupId()); - "Cannot read property 'toString' of null"
  • Retreiving the User the same way then using myWeb.ensureUser(user) to create it if it doesnt exist(even if it obviously does) - The code executes without any exeption... and nothing is put in the group

I printed out to check if every variable (group, web, user, even functions) were null, and none of them were, they all had the correct info

I'm kinda lost... any ideas ?

Here's my last code version :

ApplyPermissions()
{
    //Many commented out code
    this.SetupGroup('Visiteurs_x0020__x0028_Lecture_x', 'Visiteurs');
}

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);
    var testGroup = this.web.get_siteGroups().add(groupInfo);

    //this.AddUsersToGroup(users, newGroup);
    this.AddUsersToGroup(users, testGroup);

    return newGroup;
}

AddUsersToGroup(users, group)
{
    users.forEach((user)=>{
        var userID = user.get_lookupId();
        var userInfo = this.web.get_siteUserInfoList().getItemById(userID);
        console.log(group.get_users().addUser);

        this.context.load(userInfo);
        this.context.executeQueryAsync(()=>{
            group.get_users().addUser(userInfo);
            //I already tried putting another Query here to save, nope
        }, QueryError);
    });
}

The ultimate goal is to add the user to a subweb next, any help is extremely appreciated

  • have you tried this.. sharepoint.stackexchange.com/questions/158781/… – Shashank Reddy K Mar 2 '18 at 3:30
  • Yup, I've seen that one, the first answer is the MSDN example, wich adds a user that hasn't been created yet, the second one was really close to what i'm trying to do, but he's answer won't work since the loginNames he's using aren't including the domain, I doubt he tested it. But yeah, I tried it, the code above is quite close to it – Treycos Mar 2 '18 at 8:00
0

Finally...
Here's the final code : It is necessary to use ensureUser and call another query after adding each user

AddUsersToGroup(users, group)
{
    users.forEach((user)=>{
        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(newUser)}, QueryError);
        }, QueryError);
    });
}

I'll still accept any other answer that does the trick in an easier way, if there's one

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.