Get All Users From Site Collection Using PnP JavaScript Library

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

In this post, we will discuss how  we can get all users from Site Collection using PnP-JS-Core library.

Example

The following code snippet is used to display each user's id and display 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 users from site collection  
  9. $pnp.sp.web.siteUsers.get().then(function(data) {  
  10.     var users = "";  
  11.     for (var i = 0; i < data.length; i++) {  
  12.         users += data[i].Id + " - " + data[i].Title + "<br/>";  
  13.     }  
  14.     document.getElementById("sample").innerHTML = users;  
  15. });  
  16. </script>
Ebook Download
View all
Learn
View all