PnP JavaScript Library With jQuery In SharePoint

The PnP team has introduced a new open source JavaScript library to access SharePoint objects and this library depends on REST API Services. Due to this, the component is useful from SharePoint 2013.

In my previous posts, I explained about this library with the sample codes. The information is available at the links, given below:

In addition to this, I am adding a series of articles on using PnP library with the different JS frameworks. So far I have covered, integration with Angular JS, React JS and Knockout JS. In this article, we’ll see PnP JavaScript library with jQuery to achieve the same result (populating a sub site in a tabular format), which we obtained with other frameworks.

jQuery is one of the most widely used popular JavaScript frameworks and it is used to traverse HTML documents and provides a simplified syntax to manipulate the DOM structure in a Webpage.

What are the essential files required?

  • jQuery.js used to download the jQuery library to manipulate DOM structure.
  • Pnp.js PnP JavaScript library.
  • Fetch.js is used by PnP js files to handle the Web requests and responses (Required for IE).
  • ES6-Promise.js is used by PnP js files to handle the Web requests and responses (Required for IE).

    Note: Retrieve JS & Promise JS files are required to run PnP methods in some Browsers (IE).

In our example, we have to create a sample.html file under Style Library or Site Assets. Here, I am creating the file under Site Assets Library.

Include Script Files

I have uploaded the required script files under Site Assets library.

  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. <script type="text/javascript" src="/siteassets/scripts/jquery.min.js"></script>  
Note: I have downloaded the latest jQuery js file and referenced it in the script.

Include HTML

Add CSS styles, given below, and HTML snippet to the sample HTML file.
  1. <!-- To style table in html -->  
  2. <style type="text/css">    
  3. .web-table th{  background-color:#ddd;  border:2px solid #fff;}    
  4. .web-table td{  background-color:#eee;  border:2px solid #fff;}    
  5. .web-heading{   padding:2px;}    
  6. </style>    
  7.   
  8. <div>    
  9. <h2 class="web-heading">Sub Sites</h2>   
  10. <div>  
  11. <table width="100%" cellpadding="10" cellspacing="2" class="web-table" id="webTable">    
  12.     <thead>    
  13.     <tr>    
  14.         <th>Title</th>    
  15.         <th>Id</th>    
  16.         <th>Created</th>    
  17.         <th>Web Template</th>    
  18.     </tr>    
  19.     </thead>    
  20.     <tbody></tbody>    
  21. </table>    
  22.     <p id="countid">No websites</p>  
  23. </div>  
  24. </div>  
  25.   
  26. <script type="text/javascript">  
  27. <!- - Insert jQuery Code - - >  
  28. </script>  
Using jQuery, we will get the tbody element and add the table row, based on the desired result.

Include PnP JS Code
  1. $pnp.sp.web.webs.get().then(function(result) {  
  2.     //jQuery Code  
  3. }).catch(function(err) {  
  4.     alert(err);  
  5. });  
Webs.get() returns the collection subsites from the current Web.

Insert the jQuery code to add the row to the table.

Include jQuery Code
  1. $(document).ready(function(){  
  2. $pnp.sp.web.webs.get().then(function(result) {  
  3. if(result.length > 0)  
  4. $('#countid').html("Total subsites: "+ result.length);  
  5.   
  6.             for(var i=0; i< result.length; i++){  
  7.             $("#webTable tbody").append( "<tr>"+   
  8.             "<td>"+result[i].Title+"</td>"+   
  9.             "<td>"+result[i].Id+"</td>"+   
  10.             "<td>"+result[i].Created+"</td>"+   
  11.             "<td>"+result[i].WebTemplate+"</td>""</tr>");  
  12.             }  
  13.         }).catch(function(err){ alert(err);});  
  14.           
  15. });  
Run PnP request code after the page is fully loaded. Based on the success response, traverse HTML and select the body tag of the WebTable table and append the row. 

Full Sample Code

Insert the code, given below, within Script Editor or Content Editor Web-part.
  1. <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>  
  2. <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>  
  3. <script type="text/javascript" src="/siteassets/scripts/es6-promise.js"></script>  
  4. <script type='text/javascript' src='/SiteAssets/Scripts/jquery-3.0.0.min.js'></script>  
  5.   
  6. <style type="text/css">    
  7. .web-table th{  background-color:#ddd;  border:2px solid #fff;}    
  8. .web-table td{  background-color:#eee;  border:2px solid #fff;}    
  9. .web-heading{   padding:2px;}    
  10. </style>    
  11.   
  12. <div>    
  13. <h2 class="web-heading">Sub Sites</h2>   
  14. <div>  
  15. <table width="100%" cellpadding="10" cellspacing="2" class="web-table" id="webTable">    
  16.     <thead>    
  17.     <tr>    
  18.         <th>Title</th>    
  19.         <th>Id</th>    
  20.         <th>Created</th>    
  21.         <th>Web Template</th>    
  22.     </tr>    
  23.     </thead>    
  24.     <tbody></tbody>    
  25. </table>    
  26.     <p id="countid">No websites</p>  
  27. </div>  
  28. </div>  
  29.   
  30. <script type="text/javascript">  
  31.   
  32. $(document).ready(function(){  
  33. $pnp.sp.web.webs.get().then(function(result) {  
  34. if(result.length > 0)  
  35. $('#countid').html("Total subsites: "+ result.length);  
  36.   
  37.             for(var i=0; i< result.length; i++){  
  38.             $("#webTable tbody").append( "<tr>"+   
  39.             "<td>"+result[i].Title+"</td>"+   
  40.             "<td>"+result[i].Id+"</td>"+   
  41.             "<td>"+result[i].Created+"</td>"+   
  42.             "<td>"+result[i].WebTemplate+"</td>""</tr>");  
  43.             }  
  44.         }).catch(function(err){ alert(err);});  
  45.           
  46. });  
  47. </script>  
Output

Output

Conclusion

This article provides a simple example to list the sub sites in a table format, using jQuery framework.

 

Up Next
    Ebook Download
    View all
    Learn
    View all