How to Create a Group in SharePoint 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

The following 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.    
  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.    
  11.     //Get the URI decoded URLs.  
  12.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  13.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  14.    
  15.     // Resources are in URLs in the form:  
  16.     // web_url/_layouts/15/resource  
  17.       
  18.   
  19.     // Load the js file and continue to load the page with information about the folders.  
  20.     // SP.RequestExecutor.js to make cross-domain requests  
  21.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js",creategroup);  
  22. });  
  23.    
  24.    
  25. //Retrieve all of the folders from root Site  
  26. function creategroup()   
  27. {  
  28.     var executor;  
  29.   
  30.      // Initialize the RequestExecutor with the app web URL.  
  31.     executor = new SP.RequestExecutor(appweburl);  
  32.    
  33.     executor.executeAsync({  
  34.   
  35.   url: appweburl + "/_api/SP.AppContextSite(@target)/web/sitegroups?@target= '" + hostweburl + "'",    
  36.         method: "POST",    
  37.     
  38.   body: “{'__metadata':{ 'type''SP.Group' }, 'Title':'New Group'}”,   
  39.   
  40.         headers:     
  41.         {    
  42.             "content-type""application/json; odata=verbose"  
  43.         },     
  44.         success: function(data)    
  45.         {    
  46.             alert("Group CREATED SUCCESSFULLY ");    
  47.         },    
  48.         error: function(err)     
  49.         {    
  50.             alert("error: " + JSON.stringify(err));    
  51.         }      });  
  52. }  
  53.    
  54. //Utilities  
  55. // Retrieve a query string value.  
  56. // For production purposes you may want to use a library to handle the query string.  
  57. function getQueryStringParameter(paramToRetrieve)   
  58. {  
  59.     var params = document.URL.split("?")[1].split("&");  
  60.     for (var i = 0; i < params.length; i = i + 1)   
  61.    {  
  62.         var singleParam = params[i].split("=");  
  63.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  64.     }  
  65. }  
Publish

 

Publish the App and click the Trust it Button.


Output

Group Created Successfully.



I hope you liked this article.

Up Next
    Ebook Download
    View all
    Learn
    View all