In this blog I would like to explain share the code for create a user group in SP host web using JavaScript Object Model.
-
- var context = SP.ClientContext.get_current();
-
- $(document).ready(function () {
- console.log("page loading...")
- $("#btnCreate").click(function () {
- CreateSharePointGroup();
- });
- });
-
-
- function CreateSharePointGroup() {
-
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
- var currentWEB = appCtxSite.get_web();
-
- 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: ');
-
- var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
-
- var rdContribute = currentWEB.get_roleDefinitions().getByName("Full Control");
-
- var collContribute = SP.RoleDefinitionBindingCollection.newObject(context);
-
- collContribute.add(rdContribute);
-
- var assignments = currentWEB.get_roleAssignments();
-
- var roleAssignmentContribute = assignments.add(oMembersGRP, collContribute);
- context.load(oMembersGRP);
- context.executeQueryAsync(function () {
- alert("Sharepoint Group is created Successfully..")
- }, function (sender, args) {
- onfail(sender, args);
- });
- }
-
-
- function onfail(sender, args) {
- alert('Failed to create group. Error:' + args.get_message());
- }
-
-
- 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.