Get All Groups From Site Collection Using PnP JavaScript Library

PnP-JS-Core library contains a number of extensible methods and properties. By using this, we can achieve various actions in a simple code. To know more about this library component, visit the links given below:

In this post, we will see how to get all the groups from the Site Collection, using PnP-JS-Core library,

Example

The code snippet, given below, is used to get the group name and the group owner name from the Site Collection , using PnP JavaScript library. Click here to read more. 

  1. <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>   
  2. <script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script>   
  3. <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>  
  4.    
  5. <div id="sample"></div>  
  6.    
  7. <script type="text/javascript">  
  8. //The below PnP property used to returns all groups from site collection  
  9. $pnp.sp.web.siteGroups.get().then(function(data) {  
  10.     var groups = "";  
  11.     for (var i = 0; i < data.length; i++) {  
  12.         groups += data[i].Title + " - " + data[i].OwnerTitle + "<br/>";  
  13.     }  
  14.     document.getElementById("sample").innerHTML = groups;  
  15. });  
  16. </script>