Create User Group In SharePoint Host Web Using SharePoint Hosted App (JSOM)

In this blog I would like to explain share the code for create a user group in SP host web using JavaScript Object Model.
  1. //get Current Context  
  2. var context = SP.ClientContext.get_current();  
  3. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  4. $(document).ready(function () {  
  5. console.log("page loading...")  
  6. $("#btnCreate").click(function () {  
  7. CreateSharePointGroup();  
  8. });  
  9. });  
  10.   
  11. //this method is used to create a group  
  12. function CreateSharePointGroup() {  
  13. //Get host web URL here  
  14. var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  15. var appCtxSite = new SP.AppContextSite(context, hostWebUrl);  
  16. var currentWEB = appCtxSite.get_web();  
  17. // Create Group information for Group  
  18. var membersGRP = new SP.GroupCreationInformation();  
  19. membersGRP.set_title("GroupName");  
  20. membersGRP.set_description('Use this group to grant people full control permissions to the SharePoint site: ');  
  21. //add group  
  22. var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);  
  23. //return SP.RoleDefinition object  
  24. var rdContribute = currentWEB.get_roleDefinitions().getByName("Full Control");  
  25. // Create a new RoleDefinitionBindingCollection.  
  26. var collContribute = SP.RoleDefinitionBindingCollection.newObject(context);  
  27. // Add the role to the collection.  
  28. collContribute.add(rdContribute);  
  29. // Get the RoleAssignmentCollection for the target web.  
  30. var assignments = currentWEB.get_roleAssignments();  
  31. // assign the group to the new RoleDefinitionBindingCollection.  
  32. var roleAssignmentContribute = assignments.add(oMembersGRP, collContribute);  
  33. context.load(oMembersGRP);  
  34. context.executeQueryAsync(function () {  
  35. alert("Sharepoint Group is created Successfully..")  
  36. }, function (sender, args) {  
  37. onfail(sender, args);  
  38. });  
  39. }  
  40.   
  41. // This function is executed if the above call fails  
  42. function onfail(sender, args) {  
  43. alert('Failed to create group. Error:' + args.get_message());  
  44. }  
  45.   
  46. // this method used split the query string  
  47. function manageQueryStringParameter(paramToRetrieve) {  
  48. var params = document.URL.split("?")[1].split("&");  
  49. var strParams = "";  
  50. for (var i = 0; i < params.length; i = i + 1) {  
  51. var singleParam = params[i].split("=");  
  52. if (singleParam[0] == paramToRetrieve) {  
  53. return singleParam[1];  
  54. }  
  55. }  
  56. }  
In app manifest file provide full control permission to the site collection as shown in below,
 
 
Summary
 
In this article we have explored how to create a user group in SharePoint host web using JavaScript object model.
Ebook Download
View all
Learn
View all