Group Operations In SharePoint 2016 And Office 365 Using JavaScript Object Model

SharePoint groups were designed with the aim to manage SharePoint permissions. In a nutshell, SharePoint group is a collection of the users who share the same permission level. Assigning permission directly to the users is not considered a good practice. The users should be added to a SharePoint group, which in turn should be assigned the permissions in the SharePoint object hierarchy. In addition to it, grouping the users into the groups helps to organize and manage security; which is an important aspect of SharePoint.

SharePoint groups can be created and modified from the site permission page.

page

Let’s see how we can create and delete SharePoint groups, using JavaScript object model.

Create Group

  • Add reference to jquery file, as shown below:
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">  
    2. </script>  
    3. <script language="javascript" type="text/javascript"> 
  • Within document, call SP.SOD.executeFunc; so as to load the on demand script SP.js . Call the main starting point function say, createGroup.
    1. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createGroup);  
  • Instantiate the client context and get the Web instance. Once the Web object is retrieved, get the group collection object.
    1. var clientContext = new SP.ClientContext();  
    2. var oWeb = clientContext.get_web();  
    3.   
    4. //Get group collection  
    5. var groupCollection = oWeb.get_siteGroups();  
  • Add the new group to the collection, as shown below:
    1. var newGroup = new SP.GroupCreationInformation();  
    2. newGroup.set_title('Administrator Group');  
    3. newGroup.set_description('This group handles the access to Admin documents');  
    4. oWeb.get_siteGroups().add(newGroup);  
  • Load the client context and execute the batch which will send the request to the Server and perform the entire JavaScript object model operations as a batch.
    1. clientContext.load(groupCollection);  
    2. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  

Output

Output
Full Code

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function()  
  4.     {  
  5.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createGroup);  
  6.     });  
  7.   
  8.     function createGroup()   
  9. {  
  10.         //Get client context and web   
  11.         var clientContext = new SP.ClientContext();  
  12.         var oWeb = clientContext.get_web();  
  13.   
  14.         //Get group collection  
  15.         var groupCollection = oWeb.get_siteGroups();  
  16.   
  17.         //Add new group   
  18.         var newnewGroup = new SP.GroupCreationInformation();  
  19.         newGroup.set_title(‘Administrator Group ');  
  20.             newGroup.set_description('This group handles the access to Admin documents'); oWeb.get_siteGroups().add(newGroup);  
  21.   
  22.             //Load client context and exeecute the batch  
  23.             clientContext.load(groupCollection); clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  24.         }  
  25.   
  26.         function QuerySuccess()   
  27.         {  
  28.             console.log("New group has been created.");  
  29.         }  
  30.   
  31.         function QueryFailure(sender, args)   
  32.         {  
  33.             console.log('Request failed - ' + args.get_message());  
  34.         }  
  35. </script>  
Delete Group 
  • Follow the syntax of the full code given above, get the group collection and from that use getByName method to select the group to delete.
    1. oGroupCollection = oWeb.get_siteGroups();  
    2. oGroup = oGroupCollection.getByName("Administrator Group");  
  • Load the client context and execute the batch.
    1. clientContext.load(oGroupCollection);  
    2. clientContext.load(oGroup);  
    3. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  • In the success-call back method delete the group from the group collection.
    1. oGroupCollection.removeByLoginName(oGroup.get_loginName());  

Output

Output

Full Code

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', deleteGroup);  
  5.     });  
  6.   
  7.   
  8.     var clientContext, oGroupCollection, oGroup;  
  9.   
  10.     function deleteGroup()   
  11.     {  
  12.         //Get client context and web  
  13.         clientContext = new SP.ClientContext();  
  14.         var oWeb = clientContext.get_web();  
  15.   
  16.         //Get group collection and specific group  
  17.         oGroupCollection = oWeb.get_siteGroups();  
  18.         oGroup = oGroupCollection.getByName("Administrator Group");  
  19.   
  20.         //Load the Client Context and execute the batch  
  21.         clientContext.load(oGroupCollection);  
  22.         clientContext.load(oGroup);  
  23.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  24.     }  
  25.   
  26.     function QuerySuccess()  
  27.     {  
  28.         //Remove the group from the collection  
  29.         oGroupCollection.removeByLoginName(oGroup.get_loginName());  
  30.         clientContext.executeQueryAsync(OnQuerySuccess, OnQueryFailure);  
  31.     }  
  32.   
  33.     function QueryFailure(sender, args)  
  34.     {  
  35.         console.log('Request failed' + args.get_message());  
  36.     }  
  37.   
  38.     function OnQuerySuccess()  
  39.     {  
  40.         console.log("Group Deleted.");  
  41.     }  
  42.   
  43.     function OnQueryFailure(sender, args)   
  44.     {  
  45.         console.log('Request failed - ' + args.get_message());  
  46.     }  
  47. </script>  
Retrieve All Groups

We have discussed creation and deletion of the groups. Now, let’s see how we can retrieve all the site groups. Once the group collection is retrieved in the success- call back function we will loop through the enumerator collection to print the group names. We can test this in SharePoint by adding it to the Content Editor Web part, as shown below:
  • Save the code given below to a text file and save it into one of the SharePoint Libraries say, Site Assets.
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    2. <script language="javascript" type="text/javascript">  
    3.     $(document).ready(function() {  
    4.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getSiteGroups);  
    5.     });  
    6.   
    7.     var groupCollection;  
    8.   
    9.     function getSiteGroups()  
    10.     {  
    11.         //Get client context , web and group collection  
    12.         var clientContext = new SP.ClientContext();  
    13.         var oWeb = clientContext.get_web();  
    14.         groupCollection = oWeb.get_siteGroups();  
    15.   
    16.         //Get client context and execute the batch  
    17.         clientContext.load(groupCollection);  
    18.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    19.     }  
    20.   
    21.     function QuerySuccess()  
    22.     {  
    23.         //Get group collection and iterate through it  
    24.         var groupEnumerator = groupCollection.getEnumerator();  
    25.         console.log("The site groups available are : \n");  
    26.         while (groupEnumerator.moveNext())  
    27.         {  
    28.             var group = groupEnumerator.get_current();  
    29.             console.log(group.get_title() + "\n");  
    30.         }  
    31.     }  
    32.   
    33.     function QueryFailure(sender, args)   
    34.     {  
    35.         console.log('Request failed' + args.get_message());  
    36.     }  
    37. </script>  
  • Go to the edit settings of the SharePoint page and click Web part from the Insert tab.

    Web part
  • Add Content Editor Web part.

    Add Content Editor

  • Click Edit Web Part from Content Edit Web part. Assign the URL of the script text file and click Apply.
    Content Editor
    Click Apply and go to the console to see the output.

    output

It has printed all the site groups. Thus, we have seen how to create, retrieve and delete the site groups in SharePoint, using JavaScript Object model. It has been tested in SharePoint 2016 and SharePoint Online in Office 365.