Create A SharePoint Group Using REST API In SharePoint Online And Office 365

Welcome to an article on “How to Create a SharePoint Group using REST API in SharePoint Online and Office 365” where we will see the steps to creating an app using Napa Tool which will help us to create as many SharePoint groups as we require using REST API.

  • Open the “NAPA” Office 365 Development Tools through your SharePoint store.

  • Click on Add New Project.

  • It will ask you, what type of app do you want to build?

    Recent

  • Select SharePoint Add-in and provide a name to your app and click on Create.

    Run

    You will see the screen below on the app

    content

  • Click on Default.aspx and paste the code below under the “<asp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”.

    Code:
    1. <p <b>Create Site Group</b>  
    2.    <br />  
    3.    <input type="text" value="Enter the name of the group" id="groupname" />  
    4.    <button id="creategroupbtn">Create Group</button>  
    5. </p>  
  • Now on the navigation click on the App.js file and paste the code below removing the previous code completely.

    Code:
    1. 'use strict';  
    2. var hostweblink;  
    3. var applink;  
    4. // on load functions to be called  
    5. $(document).ready(function ()  
    6. {  
    7.     hostweblink = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
    8.     applink = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
    9.     //click function on the button click  
    10.     $("#creategroupbtn").click(function (event)  
    11.     {  
    12.         createnewgroup();  
    13.         event.preventDefault();  
    14.     });  
    15.     var scriptlink = hostweblink + "/_layouts/15/";  
    16.     $.getScript(scriptlink + "SP.RequestExecutor.js");  
    17. });  
    18. //retrieve the web link   
    19. function getQueryStringParameter(paramToRetrieve)  
    20.     {  
    21.         var paramval = document.URL.split("?")[1].split("&");  
    22.         for (var i = 0; i < paramval.length; i = i + 1)  
    23.         {  
    24.             var Paramval1 = Paramval1[i].split("=");  
    25.             if (Paramval1[0] == paramToRetrieve) return Paramval1[1];  
    26.         }  
    27.     }  
    28.     // functions to create new group  
    29. function createnewgroup()  
    30.     {  
    31.         var newgroupName = document.getElementById("groupname").value;  
    32.         var play;  
    33.         play = new SP.RequestExecutor(applink);  
    34.         play.executeAsync(  
    35.         {  
    36.             url: applink + "/_api/SP.AppContextSite(@target)/web/sitegroups?@target='" + hostweblink + "'",  
    37.             method: "POST",  
    38.             body: "{ '__metadata': { 'type': 'SP.Group' }, 'Title':'" + newgroupName + "'}",  
    39.             headers:  
    40.             {  
    41.                 "content-type""application/json; odata=verbose",  
    42.             },  
    43.             success: creategroupcompleted,  
    44.             error: creategroupdenied  
    45.         });  
    46.     }  
    47.     //creategroupcompleted  
    48. function creategroupcompleted(data)  
    49.     {  
    50.         alert("Group Created successfully")  
    51.     }  
    52.     // creategroupdenied  
    53. function creategroupdenied(data, errorCode, errorMessage)  
    54. {  
    55.     alert("Group cannot be created due to the " + errorMessage);  
    56. }  
  • Click on the settings icon on the tool on the left.

  • Under the properties, select Permissions and provide full control to the app on the Site Collection level.

    Permiaaion

  • Click on the deploy button on the left and run the project.

    Run project

  • Click on the launch button.

    Lunch app
  • Accept the trust and click on ‘Trust It’.

    Click on Trist it

  • Your app will be deployed and open for you as per the following screenshot:

    My app

  • Provide a name of the group you want to create and click on ‘Create Group’.

    Page title

  • Your group will be created by your own app and you can find the group the People and Group on your Site’s settings.My app

You will love your app. Keep reading and keep learning!

Read more articles on SharePoint: