We can use JSOM for creating custom SharePoint groups and assign permissions to groups in SharePoint 2013. I’ll explain the basics of this using CEWP and jQuery. This script can be used in SharePoint Online, SharePoint App and Farm solutions with ease.
Prerequisites: User/App must have full control on site.
Solution:
- Create a web part page in your SharePoint 2013 or Office 365 SharePoint Site.
- Add Content Editor web part on the page.
- Edit ‘HTML Source’ of content editor web part and copy the following html and hit OK.
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script src="/_layouts/15/sp.js" type="text/javascript"></script>
- <script src="/_layouts/15/SP.RequestExecutor.js" type="text/javascript"></script>
- <script src="/_layouts/15/SP.search.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function() {
- $('#btnCreateCustomGroup').click(btnCreateCustomGroup_Click);
- });
-
- function btnCreateCustomGroup_Click() {
- var grpName = "DSAuditors"
- var grpDesc = "Auditors custom group with edit access";
- var grpRole = "Edit";
- var appweburl = _spPageContextInfo.siteAbsoluteUrl;
- var clientContext = new SP.ClientContext(appweburl);
-
-
- CreateCustomGroup(clientContext, grpName, grpDesc, grpRole, true, false, true,
- function success() {
- alert("Successfully created group: " + grpName);
- },
- function fail(src, info) {
- alert("Failed to create group: " + grpName + ". " + info.get_message());
- });
-
- }
-
- function CreateCustomGroup(context, name, desc, roleName, canMembersEdit, visibleToMembersOnly, addCurrentUser, success, fail) {
- var web = context.get_web();
-
- var groupCollection = web.get_siteGroups();
-
- var newGRP = new SP.GroupCreationInformation();
- newGRP.set_title(name);
- newGRP.set_description(desc);
-
- var currentUser = web.get_currentUser();
- context.load(currentUser);
-
- context.load(web, 'Title', 'HasUniqueRoleAssignments');
- context.executeQueryAsync(function() {
-
- if (!web.get_hasUniqueRoleAssignments()) {
- web.breakRoleInheritance(true, false);
- }
-
-
- var newCreateGroup = groupCollection.add(newGRP);
-
- var rolDef = web.get_roleDefinitions().getByName(roleName);
- var rolDefColl = SP.RoleDefinitionBindingCollection.newObject(context);
- rolDefColl.add(rolDef);
-
-
- var roleAssignments = web.get_roleAssignments();
-
- roleAssignments.add(newCreateGroup, rolDefColl);
-
- newCreateGroup.set_allowMembersEditMembership(canMembersEdit);
- newCreateGroup.set_onlyAllowMembersViewMembership(visibleToMembersOnly);
-
- if (addCurrentUser) {
- newCreateGroup.get_users().addUser(currentUser);
- }
-
- newCreateGroup.update();
- context.load(newCreateGroup);
-
-
- context.executeQueryAsync(success, fail);
-
- }, fail);
- }
- </script>
- <div>
- <h1>Create SharePoint Group </h1>
- <br/>
- <input id="btnCreateCustomGroup" type="button" value="Create SPGroup" />
- </div>
- Web part page will be displayed like the following screenshot:
- Click on ‘Create SPGroup’ button to create a SharePoint group ‘DSAuditors’ with ‘Edit’ permission. It would display success/fail message.
JS explained
JS starts with required script references (jQuery, sp.js etc.). In document ready, button click event is associated to the button. Function btnCreateCustomGroup_Click get the client context and calls function createCustomGroups; which accepts various group properties as parameter.