We can use JSOM for creating custom permission sets in SharePoint 2013. I’ll explain the basics of this using a CEWP and jQuery. This script can be used in SharePoint Online, SharePoint App and Farm solutions with ease.
Prerequisites: User 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, then press 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 () {
-
- $('#btnCreateCustomPermission').click(btnCreateCustomPermission_Click);
-
- });
-
- function btnCreateCustomPermission_Click() {
- var appweburl = _spPageContextInfo.siteAbsoluteUrl;
- var clientContext = new SP.ClientContext(appweburl);
-
- var dsReadPermissions = createPermissionSet();
- createCustomPermission(clientContext, "DSRead", "DSRead", dsReadPermissions,
- function DSReadSuccess() {
- alert("Successfully created DSRead");
- },
- function DSReadFail(control, info) {
- alert("Failed to create DSRead. " + info.get_message());
- });
- }
-
- function createCustomPermission(context, name, desc, permissions, success, fail) {
-
- var roleDefinitionCreationInfo = new SP.RoleDefinitionCreationInformation();
- roleDefinitionCreationInfo.set_name(name);
- roleDefinitionCreationInfo.set_description(desc);
- roleDefinitionCreationInfo.set_basePermissions(permissions);
- var roleDefinition = context.get_site().get_rootWeb().get_roleDefinitions().add(roleDefinitionCreationInfo);
- context.executeQueryAsync(success, fail);
- }
-
- function createPermissionSet() {
-
- var permissions = new SP.BasePermissions();
- permissions.set(SP.PermissionKind.viewListItems);
- permissions.set(SP.PermissionKind.openItems);
- permissions.set(SP.PermissionKind.viewVersions);
- permissions.set(SP.PermissionKind.createAlerts);
- permissions.set(SP.PermissionKind.viewFormPages);
- permissions.set(SP.PermissionKind.createSSCSite);
- permissions.set(SP.PermissionKind.viewPages);
- permissions.set(SP.PermissionKind.browseUserInfo);
- permissions.set(SP.PermissionKind.useRemoteAPIs);
- permissions.set(SP.PermissionKind.useClientIntegration);
- permissions.set(SP.PermissionKind.open);
- permissions.set(SP.PermissionKind.managePersonalViews);
- return permissions;
- }
- </script>
- <div>
- <h1>Create Custom Permissions</h1>
- <br/>
- <input id="btnCreateCustomPermission" type="button" value="Create Permission"/>
- </div>
- Page will be displayed as in the following screenshot:
- Click on button ‘Create Permission’ to create a custom permission ‘DSRead’. 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 btnCreateCustomPermission_Click get the client context and calls function createPermissionSet; which creates all required permissions in a set. And finally function createCustomPermission gets context and permission details and creates permission level.
To check newly created permission level: Site Actions, Site Settings, then Site Permissions and click Permission Levels.