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

Publish the app and click the Trust it Button.



Output

All users are retrieved from the Group Successfully.