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.
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:
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
- </script>
- <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.
- 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.
- var clientContext = new SP.ClientContext();
- var oWeb = clientContext.get_web();
- //Get group collection
- var groupCollection = oWeb.get_siteGroups();
- Add the new group to the collection, as shown below:
- var newGroup = new SP.GroupCreationInformation();
- newGroup.set_title('Administrator Group');
- newGroup.set_description('This group handles the access to Admin documents');
- 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.
- clientContext.load(groupCollection);
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
Output

Full Code
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createGroup);
- });
- function createGroup()
- {
- //Get client context and web
- var clientContext = new SP.ClientContext();
- var oWeb = clientContext.get_web();
- //Get group collection
- var groupCollection = oWeb.get_siteGroups();
- //Add new group
- var newnewGroup = new SP.GroupCreationInformation();
- newGroup.set_title(‘Administrator Group ');
- newGroup.set_description('This group handles the access to Admin documents'); oWeb.get_siteGroups().add(newGroup);
- //Load client context and exeecute the batch
- clientContext.load(groupCollection); clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- }
- function QuerySuccess()
- {
- console.log("New group has been created.");
- }
- function QueryFailure(sender, args)
- {
- console.log('Request failed - ' + args.get_message());
- }
- </script>
- 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.
- oGroupCollection = oWeb.get_siteGroups();
- oGroup = oGroupCollection.getByName("Administrator Group");
- Load the client context and execute the batch.
- clientContext.load(oGroupCollection);
- clientContext.load(oGroup);
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- In the success-call back method delete the group from the group collection.
- oGroupCollection.removeByLoginName(oGroup.get_loginName());






RajaPosted Jun 10, 2016, 1:16 AM
Nice Share...
Debasis SahaPosted Jun 10, 2016, 12:16 AM
Good one..
Santhakumar MunuswamyPosted Jun 9, 2016, 11:58 PM
Thank you for sharing