Create SharePoint Security Group Using JSOM

We can use JSOM for creating custom SharePoint groups and assign permissions to groups in SharePoint 2013. I’ll explain the basics of this using CEWP and jQuery. This script can be used in SharePoint Online, SharePoint App and Farm solutions with ease.

Prerequisites: User/App must have full control on site.

Solution:

  1. Create a web part page in your SharePoint 2013 or Office 365 SharePoint Site.

  2. Add Content Editor web part on the page.

    Add Content Editor

  3. Edit ‘HTML Source’ of content editor web part and copy the following html and hit OK.
    1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  
    2. <script src="/_layouts/15/sp.js" type="text/javascript"></script>  
    3. <script src="/_layouts/15/SP.RequestExecutor.js" type="text/javascript"></script>  
    4. <script src="/_layouts/15/SP.search.js" type="text/javascript"></script>  
    5. <script type="text/javascript">  
    6.     $(function() {  
    7.         $('#btnCreateCustomGroup').click(btnCreateCustomGroup_Click);  
    8.     });  
    9.   
    10.     function btnCreateCustomGroup_Click() {  
    11.         var grpName = "DSAuditors"  
    12.         var grpDesc = "Auditors custom group with edit access";  
    13.         var grpRole = "Edit";  
    14.         var appweburl = _spPageContextInfo.siteAbsoluteUrl;  
    15.         var clientContext = new SP.ClientContext(appweburl);  
    16.   
    17.   
    18.         CreateCustomGroup(clientContext, grpName, grpDesc, grpRole, truefalsetrue,  
    19.             function success() {  
    20.                 alert("Successfully created group: " + grpName);  
    21.             },  
    22.             function fail(src, info) {  
    23.                 alert("Failed to create group: " + grpName + ". " + info.get_message());  
    24.             });  
    25.   
    26.     }  
    27.   
    28.     function CreateCustomGroup(context, name, desc, roleName, canMembersEdit, visibleToMembersOnly, addCurrentUser, success, fail) {  
    29.         var web = context.get_web();  
    30.         //Get all groups in site  
    31.         var groupCollection = web.get_siteGroups();  
    32.         // Create Group information for Group  
    33.         var newGRP = new SP.GroupCreationInformation();  
    34.         newGRP.set_title(name);  
    35.         newGRP.set_description(desc);  
    36.   
    37.         var currentUser = web.get_currentUser();  
    38.         context.load(currentUser);  
    39.   
    40.         context.load(web, 'Title''HasUniqueRoleAssignments');  
    41.         context.executeQueryAsync(function() {  
    42.   
    43.             if (!web.get_hasUniqueRoleAssignments()) {  
    44.                 web.breakRoleInheritance(truefalse);  
    45.             }  
    46.   
    47.             //add group to site gorup collection  
    48.             var newCreateGroup = groupCollection.add(newGRP);  
    49.             //Role Definition   
    50.             var rolDef = web.get_roleDefinitions().getByName(roleName);  
    51.             var rolDefColl = SP.RoleDefinitionBindingCollection.newObject(context);  
    52.             rolDefColl.add(rolDef);  
    53.   
    54.             // Get the RoleAssignmentCollection for the target web.  
    55.             var roleAssignments = web.get_roleAssignments();  
    56.             // assign the group to the new RoleDefinitionBindingCollection.  
    57.             roleAssignments.add(newCreateGroup, rolDefColl);  
    58.             //Set group properties  
    59.             newCreateGroup.set_allowMembersEditMembership(canMembersEdit);  
    60.             newCreateGroup.set_onlyAllowMembersViewMembership(visibleToMembersOnly);  
    61.             //add currect user to group  
    62.             if (addCurrentUser) {  
    63.                 newCreateGroup.get_users().addUser(currentUser);  
    64.             }  
    65.   
    66.             newCreateGroup.update();  
    67.             context.load(newCreateGroup);  
    68.   
    69.             //Execute Query  
    70.             context.executeQueryAsync(success, fail);  
    71.   
    72.         }, fail);  
    73.     }  
    74. </script>  
    75. <div>  
    76.     <h1>Create SharePoint Group </h1>  
    77.     <br/>  
    78.     <input id="btnCreateCustomGroup" type="button" value="Create SPGroup" />  
    79. </div>    
  4. Web part page will be displayed like the following screenshot:

    Create Custom Group

  5. Click on ‘Create SPGroup’ button to create a SharePoint group ‘DSAuditors’ with ‘Edit’ permission. It would display success/fail message.

    DSAuditors

JS explained

JS starts with required script references (jQuery, sp.js etc.). In document ready, button click event is associated to the button. Function btnCreateCustomGroup_Click get the client context and calls function createCustomGroups; which accepts various group properties as parameter.

Up Next
    Ebook Download
    View all
    Learn
    View all