none
Adding users RRS feed

  • Question

  • Dear all,

    I have a requirement like i want to add users to site collection using CSOM,I want to provide a UI to add users and before adding i need to validate the user and after adding i want to add to contributors group using csom.Please help me how to implemet this.

    Regards,

    Praveen

    Tuesday, December 11, 2018 2:27 AM

Answers

  • Hi Praveenrangu,

    You could create a Client People Picker in a page to input, validate user and then add to a SharePoint site Group, here is a code demo for your reference:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=10" />
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="/_layouts/15/1033/styles/Themable/corev15.css" />
        <script type="text/javascript" src="/_layouts/15/init.js"></script>
        <script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
        <script type="text/javascript" src="/_layouts/15/1033/initstrings.js"></script>
        <script type="text/javascript" src="/_layouts/15/1033/strings.js"></script>
        <script type="text/javascript" src="/_layouts/15/SP.UI.Controls.js"></script>
        <script type="text/javascript" src="/_layouts/15/clienttemplates.js"></script>
        <script type="text/javascript" src="/_layouts/15/clientforms.js"></script>
        <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
        <script type="text/javascript" src="/_layouts/15/autofill.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.ui.dialog.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.js"></script>
        <script type="text/javascript" src="/_layouts/15/core.js"></script>
        <script type="text/javascript" src="/_layouts/15/SP.Core.JS"></script>
    
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      
        <script type="text/javascript">
            $(function () {
                initializePeoplePicker('peoplePickerDiv');
    
            });
            function initializePeoplePicker(peoplePickerElementId) {
                // Create a schema to store picker properties, and set the properties.
                var schema = {};
                schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
                schema['SearchPrincipalSource'] = 15;
                schema['ResolvePrincipalSource'] = 15;
                schema['AllowMultipleValues'] = true;
                schema['MaximumEntitySuggestions'] = 50;
                schema['Width'] = '280px';
                // Render and initialize the picker.
                // Pass the ID of the DOM element that contains the picker, an array of initial
                // PickerEntity objects to set the picker value, and a schema that defines
                // picker properties.
                this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
    
            }
            function getUserInfo() {
    
                // Get the people picker object from the page.
                var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
    
                // Get information about all users.
                var users = peoplePicker.GetAllUserInfo();
                var userInfo = '';
                for (var i = 0; i < users.length; i++) {
                    var user = users[i];
                    //console.log(user);
                    var title= user["AutoFillDisplayText"];
                    var loginname= user["AutoFillKey"];
                    var email= user.EntityData["Email"];
                    addUserToSharePointGroup(12,email,loginname,title);
                }
               
            }
    
    
            function addUserToSharePointGroup(GroupId, email, loginName, Title) {
    
                var clientContext = new SP.ClientContext.get_current();
                var collGroup = clientContext.get_web().get_siteGroups();
                var oGroup = collGroup.getById(GroupId);
                var userCreationInfo = new SP.UserCreationInformation();
                userCreationInfo.set_email(email);
                userCreationInfo.set_loginName(loginName);
                userCreationInfo.set_title(Title);
                this.oUser = oGroup.get_users().add(userCreationInfo);
    
                clientContext.load(oUser);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
            }
    
            function onQuerySucceeded() {
    
                alert(this.oUser.get_title() + " added.");
            }
    
            function onQueryFailed(sender, args) {
    
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
           
        </script>
    
    </head>
    
    <body>
            <div>
    
                <table>
    
                    <tr>
                        <td><strong>User Name: </strong></td>
                        <td><div id="peoplePickerDiv"></div></td>
                    </tr>
    
                    <tr>
                        <td><input id="btnGetUser" type="button" value="AddUserToGroup" onclick="getUserInfo()" /></td>
                    </tr>
                </table>
            </div>
    </body>
    
    </html>

    In the code snippet demo, I am adding the user "Jerry" into Approver Group which Id is 12, you can use your own Group Id in the code above to make it work:


    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    Tuesday, December 11, 2018 8:10 AM

All replies

  • https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code#sharepoint-user-tasks

    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, December 11, 2018 3:15 AM
  • Thanks justin for your response. Iam planning to create a visual webpart and adding textbox and button controls.On button click how to validate the user before adding to the group  whether user exists or not if valid user then only we can add.Please help.Thanks for your help.

    Tuesday, December 11, 2018 4:46 AM
  • Hi

    To validate a user, you can use Web.EnsureUser method.

    https://sharepoint.stackexchange.com/questions/130708/csom-or-rest-to-verify-user/130709


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, December 11, 2018 5:39 AM
  • Hi Praveenrangu,

    You could create a Client People Picker in a page to input, validate user and then add to a SharePoint site Group, here is a code demo for your reference:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=10" />
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="/_layouts/15/1033/styles/Themable/corev15.css" />
        <script type="text/javascript" src="/_layouts/15/init.js"></script>
        <script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
        <script type="text/javascript" src="/_layouts/15/1033/initstrings.js"></script>
        <script type="text/javascript" src="/_layouts/15/1033/strings.js"></script>
        <script type="text/javascript" src="/_layouts/15/SP.UI.Controls.js"></script>
        <script type="text/javascript" src="/_layouts/15/clienttemplates.js"></script>
        <script type="text/javascript" src="/_layouts/15/clientforms.js"></script>
        <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
        <script type="text/javascript" src="/_layouts/15/autofill.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.ui.dialog.js"></script>
        <script type="text/javascript" src="/_layouts/15/sp.js"></script>
        <script type="text/javascript" src="/_layouts/15/core.js"></script>
        <script type="text/javascript" src="/_layouts/15/SP.Core.JS"></script>
    
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      
        <script type="text/javascript">
            $(function () {
                initializePeoplePicker('peoplePickerDiv');
    
            });
            function initializePeoplePicker(peoplePickerElementId) {
                // Create a schema to store picker properties, and set the properties.
                var schema = {};
                schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
                schema['SearchPrincipalSource'] = 15;
                schema['ResolvePrincipalSource'] = 15;
                schema['AllowMultipleValues'] = true;
                schema['MaximumEntitySuggestions'] = 50;
                schema['Width'] = '280px';
                // Render and initialize the picker.
                // Pass the ID of the DOM element that contains the picker, an array of initial
                // PickerEntity objects to set the picker value, and a schema that defines
                // picker properties.
                this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
    
            }
            function getUserInfo() {
    
                // Get the people picker object from the page.
                var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
    
                // Get information about all users.
                var users = peoplePicker.GetAllUserInfo();
                var userInfo = '';
                for (var i = 0; i < users.length; i++) {
                    var user = users[i];
                    //console.log(user);
                    var title= user["AutoFillDisplayText"];
                    var loginname= user["AutoFillKey"];
                    var email= user.EntityData["Email"];
                    addUserToSharePointGroup(12,email,loginname,title);
                }
               
            }
    
    
            function addUserToSharePointGroup(GroupId, email, loginName, Title) {
    
                var clientContext = new SP.ClientContext.get_current();
                var collGroup = clientContext.get_web().get_siteGroups();
                var oGroup = collGroup.getById(GroupId);
                var userCreationInfo = new SP.UserCreationInformation();
                userCreationInfo.set_email(email);
                userCreationInfo.set_loginName(loginName);
                userCreationInfo.set_title(Title);
                this.oUser = oGroup.get_users().add(userCreationInfo);
    
                clientContext.load(oUser);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
            }
    
            function onQuerySucceeded() {
    
                alert(this.oUser.get_title() + " added.");
            }
    
            function onQueryFailed(sender, args) {
    
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
           
        </script>
    
    </head>
    
    <body>
            <div>
    
                <table>
    
                    <tr>
                        <td><strong>User Name: </strong></td>
                        <td><div id="peoplePickerDiv"></div></td>
                    </tr>
    
                    <tr>
                        <td><input id="btnGetUser" type="button" value="AddUserToGroup" onclick="getUserInfo()" /></td>
                    </tr>
                </table>
            </div>
    </body>
    
    </html>

    In the code snippet demo, I am adding the user "Jerry" into Approver Group which Id is 12, you can use your own Group Id in the code above to make it work:


    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    Tuesday, December 11, 2018 8:10 AM
  • thanku very much jerry this is working in sharepoint online fine.how could i implement this 2010.
    Tuesday, December 11, 2018 9:21 AM