Get Permission Levels Using REST API

I have created a simple code snippet which contains the combination of HTML structure, styles required for styling html structure and main JavaScript code.

HTML DOM

  1. <!-- Style required for HTML Snippet -->  
  2. < style type = "text/css" > .tbl - roles th  
  3.     {  
  4.         background - color: #ddd;  
  5.         border: 2 px solid# fff;  
  6.         text - align: left  
  7.     }.tbl - roles td  
  8.     {  
  9.         background - color: #eee;  
  10.         border: 2 px solid# fff;  
  11.     }.web - heading  
  12.     {  
  13.         padding: 2 px;  
  14.     } < /style>  
  15.     <!--Include jQuery library to perform dynamic html dom manipulation -->  
  16.     < script type = "text/javascript"  
  17. src = "/siteassets/scripts/jquery.js" > < /script> < div > < h2 class = "web-heading" > Web Role definitions < /h2> < /div> < table width = "100%"  
  18. cellpadding = "10"  
  19. cellspacing = "2"  
  20. id = "tblRoles"  
  21. class = "tbl-roles" > < thead > < tr > < th > Id < /th> < th > Role Name < /th> < th > Description < /th> < /tr> < /thead> < tbody > < /tbody> < /table>  
  22. Load  
  1. Some methods from jQuery is used to generate the result in tabular format.
  2. Create skeleton of table structure. Header row alone, later with the help of jQuery we will generate other rows.
  3. CSS lines help to style the table with results.

The permission level refers to the roles that are associated in the SharePoint site, which is used to align the permissions to groups or users to the SharePoint object.

The following REST API endpoint returns the collection of permission levels associated to the SharePoint site.

  1. http://<SharePoint site>/_api/web/roleDefinitions  
JS Snippet
  1. <!-- Following script returns the collection of role definitions of a web -->  
  2. < script type = "text/javascript" > functiongetRoles()  
  3. {  
  4.     varsiteurl = _spPageContextInfo.webAbsoluteUrl;  
  5.     $.ajax(  
  6.     {  
  7.         url: siteurl + "/_api/web/roleDefinitions",  
  8.         method: "GET",  
  9.         headers:  
  10.         {  
  11.             "Accept""application/json; odata=verbose"  
  12.         },  
  13.         success: function(data)  
  14.         {  
  15.             // Returning the results   
  16.             onSucceed(data);  
  17.         },  
  18.         error: function(data)  
  19.         {  
  20.             onFailure(data);  
  21.         }  
  22.     });  
  23. }  
  24. functiononSucceed(data)  
  25. {  
  26.     //Collection of role definitions returned as a result   
  27.     varroledefs = data.d.results;  
  28.     for (i = 0; i < roledefs.length; i++)  
  29.     {  
  30.         vartrow = "<tr><td>" + roledefs[i].Id + "</td><td>" + roledefs[i].Name + "</td><td>" + roledefs[i].Description + "</td>" + "</tr>";  
  31.         //Append each role definition to the table as a row   
  32.         $("#tblRolestbody").append(trow);  
  33.     }  
  34. }  
  35. functiononFailure(error)  
  36. {  
  37.     alert(error);  
  38. }  
  39. functioninjectMethod()  
  40. {  
  41.     getRoles();  
  42. }  
  43. injectMethod(); < /script> 
  1. By using the jQuery’s $.ajax method, we will send the REST url to the server as a request.
  2. The success parameter of $.ajax returns the response.
  3. In the success method, we have written the code to extract the result and populate the returned data in a table.

Add the snippet to the content editor as I mentioned in the article. You can also try the code in SharePoint add-in to get the following result.

Output

The code is tested in SharePoint online and the following screenshot shows the results in tabular format.

WEB ROLE