Get All Site Groups in SharePoint using JSOM

The example in this topic show how to use JSOM to Get All Site Groups in SharePoint

  • var oGroupCollection, clientContext;
  • function GetAllSiteGroups() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     // Get Site Group Collection object
  •     oGroupCollection = clientContext.get_web().get_siteGroups();
  •  
  •     // Load Site groups
  •     clientContext.load(oGroupCollection);
  •    
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     // Iterate through each item
  •     var oEnumerator = oGroupCollection.getEnumerator();
  •  
  •     while (oEnumerator.moveNext()) {
  •         var oGroup = oEnumerator.get_current();
  •         console.log("Title of the group is: " + oGroup.get_title() + "\n");
  •     }
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint