Steps for Implementation
- Get Current Context.
- Get App web URL and Host Web URL from Query string parameter.
- Calling CreateMultipleGroup method in document ready.
- Get web from app context site.
- Create new group Information
- Get Role definition by name (permission for the Group)
- Add group and add the role to the collection.
- Set Allow membership then set Group Owner
- Push oMembersGRP in array
- Then load the array
- Finally execute
In your JavaScript file write the following code,
- // Js code Starts here
- 'use strict';
- //Get Current Context
- var context = SP.ClientContext.get_current();
- //Declaring the variables
- varhostWebURL, appWebURL;
- // this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
- $(document).ready(function () {
- // Get App web URL and Host Web URL from Query string parameter
- hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
- //Calling Bulk Edit method in document ready
- CreateMultipleGroup ();
- });
- function CreateMultipleGroup()
- {
- var permission = ["TestGroup", "TestGroup1", "TestGroup2"];
- var membershipArray = [];
- var deferred = $.Deferred();
- var currentWEB = context.get_web();
- // Create Group information for Group
- var membersGRP = new SP.GroupCreationInformation();
- // Create a new RoleDefinitionBindingCollection.
- var rdContribute = currentWEB.get_roleDefinitions().getByName("Contribute");
- for (var i = 0; i < permission.length; i++)
- {
- var collContribute = SP.RoleDefinitionBindingCollection.newObject(context);
- membersGRP.set_title(permission[i]);
- membersGRP.set_description('Use this group to grant people contribute permissions to the SharePoint site:');
- //add group
- var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
- // Add the role to the collection.
- collContribute.add(rdContribute);
- //set Allow membership edit to false
- oMembersGRP.set_allowMembersEditMembership(false);
- //Set group Wowner
- var groupOwner = currentWEB.get_siteGroups().getByName("GroupName");
- oMembersGRP.set_owner(groupOwner);
- oMembersGRP.set_onlyAllowMembersViewMembership(false);
- oMembersGRP.update();
- //Push membership in array
- membershipArray.push(oMembersGRP);
- context.load(membershipArray[membershipArray.length - 1]);
- }
- //Execute Query
- context.executeQueryAsync(
- function () {
- deferred.resolve();
- alert("Group Created Successfully");
- },
- function (sender, args) {
- deferred.reject();
- alert("Failed to create groups " + args.get_message());
- });
- return deferred.promise();
- }
- //method for read query string value
- function manageQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) {
- return singleParam[1];
- }
- }
- }
In this blog we have explored how to create multiple SP user group in single Asynchronous process using JSOM. If we follow the single asynchronous process for creating multiple groups the performance will improve. Happy Coding!

Kaviya BalasubramanianPosted Feb 16, 2017, 3:19 AM
Sry i missed to change the variable name .
Carlos GibbonsPosted Feb 15, 2017, 5:33 PM
Where the appCtx variable is defined ?
Kaviya BalasubramanianPosted Jul 11, 2016, 12:18 AM
Thanks Santhakumar :)
Santhakumar MunuswamyPosted Jan 9, 2016, 3:14 AM
Thanks for nice share