Create Role Definition In SharePoint 2016 And Office 365 Using JavaScript Object Model

Security management is a prime concern in SharePoint as the right content has to be served to the right people with adequate permissions. SharePoint recommends assigning role-based permissions. All the permissions are managed through the roles. Roles are classified into two sections:

  • Role Definition and
  • Role Assignment

Role definition, also known as a permission level, is the list of the permissions associated with the role. Full control, contribute, read, design and limited access are some of the role definitions available. Role assignment is the relationship established between the users/groups and the role definition. Hence, when we assign a role programmatically, it is a two-step process: instantiation of the role definition and implementation of the role assignment to the user/group.

In this article, we will see how to create a role definition with the specific permissions, using JavaScript Object model.

Internal Implementation

  • Add reference to jQuery file.
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>   
    2. <script language="javascript" type="text/javascript">   
  • Within the document ready function, call SP.SOD.executeFunc, so as to load the on demand script SP.js . Call the main starting point function, say: createRoleDef.
    1. SP.SOD.executeFunc('sp.js''SP.ClientContext', createRoleDef);   
  • Instantiate client context and get the site instance.
    1. var clientContext = new SP.ClientContext();   
    2. var oSiteColl = clientContext.get_site();   
  • Create the permissions object. Once the permissions object is created, assign the required permissions to it.
    1. var oWeb = clientContext.get_web();   
    2. var oPermissions = new SP.BasePermissions();   
    3. //Set the permission masks   
    4. oPermissions.set(SP.PermissionKind.viewListItems);   
    5. oPermissions.set(SP.PermissionKind.addListItems);   
    6. oPermissions.set(SP.PermissionKind.editListItems);  
    7. oPermissions.set(SP.PermissionKind.deleteListItems);  
  • Create the role definition object and add it to the site object.
    1. var oRoleDefinitionCreationInfo = new SP.RoleDefinitionCreationInformation();   
    2. oRoleDefinitionCreationInfo.set_name('Custom Role Definition');   
    3. oRoleDefinitionCreationInfo.set_description('Custom Role Definition to manage list items');   
    4. oRoleDefinitionCreationInfo.set_basePermissions(oPermissions);   
    5. var roleDefinition = oSiteColl.get_rootWeb().get_roleDefinitions().add(oRoleDefinitionCreationInfo);   
  • Load the client context and execute the batch, which will send the request to the Server and perform the entire JavaScript object model operation as a batch.
    1. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  

Full Code

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         SP.SOD.executeFunc('sp.js''SP.ClientContext', createRoleDef);  
  5.     });  
  6.     var roleDefCollection;  
  7.   
  8.     function createRoleDef() {  
  9.         //Get the client context and site object   
  10.         var clientContext = new SP.ClientContext();  
  11.         var oSiteColl = clientContext.get_site();  
  12.         //Create a permissions object   
  13.         var oPermissions = new SP.BasePermissions();  
  14.         //Set the permission masks   
  15.         oPermissions.set(SP.PermissionKind.viewListItems);  
  16.         oPermissions.set(SP.PermissionKind.addListItems);  
  17.         oPermissions.set(SP.PermissionKind.editListItems);  
  18.         oPermissions.set(SP.PermissionKind.deleteListItems);  
  19.         //Create a new role definition.   
  20.         var oRoleDefinitionCreationInfo = new SP.RoleDefinitionCreationInformation();  
  21.         oRoleDefinitionCreationInfo.set_name('Custom Role Definition');  
  22.         oRoleDefinitionCreationInfo.set_description('Custom Role Definition to manage list items');  
  23.         oRoleDefinitionCreationInfo.set_basePermissions(oPermissions);  
  24.         var roleDefinition = oSiteColl.get_rootWeb().get_roleDefinitions().add(oRoleDefinitionCreationInfo);  
  25.         //Execute the batch   
  26.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  27.     }  
  28.   
  29.     function QuerySuccess() {  
  30.         console.log("New Role Definition has been created.");  
  31.     }  
  32.   
  33.     function QueryFailure(sender, args) {  
  34.         console.log('Request failed' + args.get_message());  
  35.     }  
  36. </script>  
We can test this in SharePoint by adding the script to the Content Editor Web part, as shown below:

SharePoint Implementation

 

  • Save the code, mentioned above, to a text file and save it into one of the SharePoint Libraries, say: Site Assets.

  • Go to the edit settings of SharePoint page and click Web part from the Insert tab.

    Web part

  • Add Content Editor Web part.

    Content Editor

  • Click Edit Web art from Content Edit Web part. Assign the URL of the script text file and click Apply.

    Content Edit Web part

    Click Apply. This will create the new role definition. We can check this from the site permissions page.

Output

Output

Summary

Thus, we have seen, how to create a new role definition in SharePoint, using JavaScript object model. This has been tested in both SharePoint 2016 and Office 365.

Up Next
    Ebook Download
    View all
    Learn
    View all