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

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. function createGroup()
  8. {
  9. //Get client context and web
  10. var clientContext = new SP.ClientContext();
  11. var oWeb = clientContext.get_web();
  12. //Get group collection
  13. var groupCollection = oWeb.get_siteGroups();
  14. //Add new group
  15. var newnewGroup = new SP.GroupCreationInformation();
  16. newGroup.set_title(‘Administrator Group ');
  17. newGroup.set_description('This group handles the access to Admin documents'); oWeb.get_siteGroups().add(newGroup);
  18. //Load client context and exeecute the batch
  19. clientContext.load(groupCollection); clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  20. }
  21. function QuerySuccess()
  22. {
  23. console.log("New group has been created.");
  24. }
  25. function QueryFailure(sender, args)
  26. {
  27. console.log('Request failed - ' + args.get_message());
  28. }
  29. </script>
Delete Group

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. var clientContext, oGroupCollection, oGroup;
  7. function deleteGroup()
  8. {
  9. //Get client context and web
  10. clientContext = new SP.ClientContext();
  11. var oWeb = clientContext.get_web();
  12. //Get group collection and specific group
  13. oGroupCollection = oWeb.get_siteGroups();
  14. oGroup = oGroupCollection.getByName("Administrator Group");
  15. //Load the Client Context and execute the batch
  16. clientContext.load(oGroupCollection);
  17. clientContext.load(oGroup);
  18. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  19. }
  20. function QuerySuccess()
  21. {
  22. //Remove the group from the collection
  23. oGroupCollection.removeByLoginName(oGroup.get_loginName());
  24. clientContext.executeQueryAsync(OnQuerySuccess, OnQueryFailure);
  25. }
  26. function QueryFailure(sender, args)
  27. {
  28. console.log('Request failed' + args.get_message());
  29. }
  30. function OnQuerySuccess()
  31. {
  32. console.log("Group Deleted.");
  33. }
  34. function OnQueryFailure(sender, args)
  35. {
  36. console.log('Request failed - ' + args.get_message());
  37. }
  38. </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:

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.