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.


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.


Default Aspx



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



Output: Role permission retrieved Successfully.