Retrieve All Groups in SharePoint 2013 Root Site Using REST API

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 following source code.
  • Publish Your App.



Prerequisite

These are important steps 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.



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", retrivegroup);  
  15. });  
  16. //Retrieve all of the folders from root Site    
  17. function retrivegroup()   
  18. {  
  19.     var executor;  
  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?@target= '" + hostweburl + "'",  
  24.         method: "GET",  
  25.   
  26.         headers: {  
  27.             "Accept""application/json; odata=verbose"  
  28.         },  
  29.         success: getGroupsSuccessHandler,  
  30.         error: function(err) {  
  31.             alert("error: " + JSON.stringify(err));  
  32.         }  
  33.     });  
  34. }  
  35.   
  36. function getGroupsSuccessHandler(data)   
  37. {  
  38.     var jsonObject = JSON.parse(data.body);  
  39.     var Groups = document.getElementById("RetriveGroups");  
  40.     if (Groups.hasChildNodes())   
  41.     {  
  42.         while (Groups.childNodes.length >= 1)   
  43.         {  
  44.             Groups.removeChild(Groups.firstChild);  
  45.         }  
  46.     }  
  47.     var results = jsonObject.d.results;  
  48.     for (var i = 0; i < results.length; i++)   
  49.     {  
  50.         var allgroups = document.createElement("option");  
  51.         allgroups.value = results[i].Title;  
  52.         allgroups.innerText = results[i].Title;  
  53.         Groups.appendChild(allgroups);  
  54.     }  
  55. }  
  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

Group Retrieved Successfully.