Create Multiple SharePoint user group using JSOM

In this blog, I would like to share the way for create multiple SharePoint user Group in Single Asynchronous process using JavaScript object model.

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,

  1. // Js code Starts here   
  2. ‘use strict’;
  3. //Get Current Context   
  4. var context = SP.ClientContext.get_current();
  5. //Declaring the variables   
  6. varhostWebURL, appWebURL;
  7. // this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model   
  8. $(document).ready(function () {
  9.     // Get App web URL and Host Web URL from Query string parameter   
  10.     hostWebURL = decodeURIComponent(manageQueryStringParameter(‘SPHostUrl’));
  11.     appWebURL = decodeURIComponent(manageQueryStringParameter(‘SPAppWebUrl’));
  12.     //Calling Bulk Edit method in document ready   
  13.     CreateMultipleGroup ();
  14. });
  15. function CreateMultipleGroup()
  16. {
  17.     var permission = [“TestGroup”“TestGroup1”“TestGroup2”];
  18.     var membershipArray = [];
  19.     var deferred = $.Deferred();
  20.     //var appCtxSite = new SP.AppContextSite(context, hostWebURL);
  21.     var currentWEB = appCtx.get_web();
  22.     // Create Group information for Group
  23.     var membersGRP = new SP.GroupCreationInformation();
  24.     // Create a new RoleDefinitionBindingCollection.   
  25.     var rdContribute = currentWEB.get_roleDefinitions().getByName(“Contribute”);
  26.     for (var i = 0; i < permission.length; i++)
  27.     {
  28.         var collContribute = SP.RoleDefinitionBindingCollection.newObject(context);
  29.         membersGRP.set_title(permission[i]);
  30.         membersGRP.set_description(‘Use this group to grant people contribute permissions to the SharePoint site:’);
  31.         //add group
  32.         var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
  33.         // Add the role to the collection.
  34.         collContribute.add(rdContribute);
  35.         //set Allow membership edit to false
  36.         oMembersGRP.set_allowMembersEditMembership(false);
  37.         //Set group Wowner
  38.         var groupOwner = currentWEB.get_siteGroups().getByName(“GroupName”);
  39.         oMembersGRP.set_owner(groupOwner);
  40.         oMembersGRP.set_onlyAllowMembersViewMembership(false);
  41.         oMembersGRP.update();
  42.         //Push membership in array 
  43.         membershipArray.push(oMembersGRP);
  44.         context.load(membershipArray[membershipArray.length – 1]);
  45.     }
  46.     //Execute Query
  47.     context.executeQueryAsync(
  48.         function () {
  49.             deferred.resolve();
  50.             alert(“Group Created Successfully”);
  51.         },
  52.         function (sender, args) {
  53.             deferred.reject();
  54.             alert(“Failed to create groups “ + args.get_message());
  55.         });
  56.     return deferred.promise();
  57. }
  58. //method for read query string value
  59. function manageQueryStringParameter(paramToRetrieve) {
  60.     var params = document.URL.split(“?”)[1].split(“&”);
  61.     var strParams = “”;
  62.     for (var i = 0; i < params.length; i = i + 1) {
  63.         var singleParam = params[i].split(“=”);
  64.         if (singleParam[0] == paramToRetrieve) {
  65.             return singleParam[1];
  66.         }
  67.     }
  68. }

Summary

In this article 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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s