In this blog I would like to explain share the code for create a user group in SP host web using JavaScript Object Model.
- //get Current Context
- var context = SP.ClientContext.get_current();
- // 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 () {
- console.log("page loading...")
- $("#btnCreate").click(function () {
- CreateSharePointGroup();
- });
- });
- //this method is used to create a group
- function CreateSharePointGroup() {
- //Get host web URL here
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
- var currentWEB = appCtxSite.get_web();
- // Create Group information for Group
- var membersGRP = new SP.GroupCreationInformation();
- membersGRP.set_title("GroupName");
- membersGRP.set_description('Use this group to grant people full control permissions to the SharePoint site: ');
- //add group
- var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
- //return SP.RoleDefinition object
- var rdContribute = currentWEB.get_roleDefinitions().getByName("Full Control");
- // Create a new RoleDefinitionBindingCollection.
- var collContribute = SP.RoleDefinitionBindingCollection.newObject(context);
- // Add the role to the collection.
- collContribute.add(rdContribute);
- // Get the RoleAssignmentCollection for the target web.
- var assignments = currentWEB.get_roleAssignments();
- // assign the group to the new RoleDefinitionBindingCollection.
- var roleAssignmentContribute = assignments.add(oMembersGRP, collContribute);
- context.load(oMembersGRP);
- context.executeQueryAsync(function () {
- alert("Sharepoint Group is created Successfully..")
- }, function (sender, args) {
- onfail(sender, args);
- });
- }
- // This function is executed if the above call fails
- function onfail(sender, args) {
- alert('Failed to create group. Error:' + args.get_message());
- }
- // this method used split the query string
- 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 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.

Kaviya BalasubramanianPosted Sep 21, 2016, 1:06 AM
Thanks Humayun
Humayun Kabir MamunPosted Sep 21, 2016, 1:03 AM
Helpful...
Kaviya BalasubramanianPosted Sep 20, 2016, 2:11 AM
Thanks Tarun
Tarun DPosted Sep 20, 2016, 12:57 AM
Nice one.Thanks for sharing..