Retrieving All Permission Levels in SharePoint Using REST

This article shows how to retrieve all permission levels in SharePoint using REST. Develop the project using the following method in the NAPA Tool.

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button
  • Replace APP.js with the source code below
  • Publish Your App.


Prerequisites

The following is an important procedure to be done before creating the app. Specify the permissions that your app needs as in the following. Choose the Properties button at the bottom of the page.
  • In the Properties window, choose Permissions.
  • In the Content category, set the Write permissions for the Tenant scope.
  • In the Social category, set the Read permissions for the User Profiles scope.
  • Close the Properties window.


Default Aspx



Source Code
  1. 'use strict';  
  2.    
  3. var hostweburl;  
  4. var appweburl;  
  5.    
  6.  // Get the URLs for the app web the host web URL from the query string.  
  7.   
  8. $(document).ready(function () {  
  9.    
  10.     //Get the URI decoded URLs.  
  11.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  12.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  13.    
  14.     // Resources are in URLs in the form:  
  15.     // web_url/_layouts/15/resource  
  16.       
  17.   
  18.     // Load the js file and continue to load the page with information about the folders.  
  19.     // SP.RequestExecutor.js to make cross-domain requests  
  20.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js",getuserpermission);  
  21. });  
  22.    
  23.    
  24. function getuserpermission () {  
  25.     var executor;  
  26.    //var userEmail="[email protected]";  
  27.      // Initialize the RequestExecutor with the app web URL.  
  28.     executor = new SP.RequestExecutor(appweburl);  
  29.    
  30.     executor.executeAsync({  
  31.   
  32.     url: appweburl + "/_api/SP.AppContextSite(@target)/web/RoleDefinitions?@target='" + hostweburl + "'",  
  33.     method: "GET",  
  34.     headers: {  
  35.     "Accept""application/json; odata=verbose"  
  36.     },  
  37.    success: getPermissionLevelsSuccessHandler,  
  38.     error: getPermissionLevelsErrorHandler  
  39.     });  
  40. }  
  41.   
  42. //Populate the selectPermissionLevels control after retrieving all of the site permission levels.  
  43. function getPermissionLevelsSuccessHandler(data) {  
  44.     var jsonObject = JSON.parse(data.body);  
  45.     var PermissionLevels = document.getElementById("retrivePermissionLevels");  
  46.    
  47.     if (PermissionLevels.hasChildNodes()) {  
  48.         while (PermissionLevels.childNodes.length >= 1) {  
  49.         PermissionLevels.removeChild(PermissionLevels.firstChild);  
  50.     }  
  51. }  
  52. var results = jsonObject.d.results;  
  53. for (var i = 0; i < results.length; i++) {  
  54.     var retrivegroup = document.createElement("option");  
  55.    retrivegroup.value = results[i].Name;  
  56.    retrivegroup.innerText = results[i].Name;  
  57.     PermissionLevels.appendChild(retrivegroup);  
  58.     }  
  59. }  
  60. function getPermissionLevelsErrorHandler (data, errorCode, errorMessage) {  
  61. alert("Could not get site permission levels: " + errorMessage);  
  62. }  
  63.   
  64.  //Utilities  
  65. // Retrieve a query string value.  
  66. // For production purposes you may want to use a library to handle the query string.  
  67. function getQueryStringParameter(paramToRetrieve) {  
  68.     var params = document.URL.split("?")[1].split("&");  
  69.     for (var i = 0; i < params.length; i = i + 1) {  
  70.         var singleParam = params[i].split("=");  
  71.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  72.     }  
  73. }  
Publish: Publish the app and click the Trust it Button.



Output: Role permission retrieved Successfully.