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?


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


You will see the screen below on the app


  • Click on Default.aspx and paste the code below under the

  1. <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 $(document).ready(function ()  

  5. {      

  6. hostweblink = decodeURIComponent(getQueryStringParameter("SPHostUrl"));      

  7. applink = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  

  8. //click function on the button click     

  9. $("#creategroupbtn").click(function (event)      

  10. {          

  11. createnewgroup();  

  12. event.preventDefault();      

  13. });      

  14. var scriptlink = hostweblink + "/_layouts/15/";      

  15. $.getScript(scriptlink + "SP.RequestExecutor.js");  

  16. });  

  17. //retrieve the web link 

  18. function getQueryStringParameter(paramToRetrieve)      

  19. {          

  20. var paramval = document.URL.split("?")[1].split("&");  

  21. for (var i = 0; i < paramval.length; i = i + 1)          

  22. {              

  23. var Paramval1 = Paramval1[i].split("=");  

  24. if (Paramval1[0] == paramToRetrieve) 

  25. 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. url: applink + "/_api/SP.AppContextSite(@target)/web/sitegroups @target='" + hostweblink + "'",              

  36. method: "POST",              

  37. body: "{ '__metadata': { 'type': 'SP.Group' }, 'Title':'" + newgroupName + "'}",             

  38.  headers:              

  39. {  

  40. "content-type": "application/json; odata=verbose",              

  41. },              

  42. success: creategroupcompleted,              

  43. error: creategroupdenied          

  44. });      

  45. }  

  46. //creategroupcompleted

  47. function creategroupcompleted(data)      

  48. {          

  49. alert("Group Created successfully")      

  50. }  

  51. // creategroupdenied

  52. function creategroupdenied(data, errorCode, errorMessage)  

  53. {      

  54. alert("Group cannot be created due to the " + errorMessage);  

  55. }  


  • 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.


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





  • Click on the launch button.


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


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


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


  • 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!