Populate Sharepoint Sub Sites Using Pnp JS And Office UI Components

Office UI Fabric

Office UI Fabric is a CSS framework, similar to Bootstrap. Office UI framework is used as a front-end model for Office applications. This has its own styling for responsive Grid and components for building modern web and Office applications. This fabric styling and components are used as primary core for developing modern UI experience in SharePoint. Fabric framework is available in different flavors also, using jQuery, React and Angular JS. So, this enables the UI designers or developers to choose their choice in developing the modern applications.

I’m gonna start a series of integrating Office UI fabric components and PnP JS library to access & manage the SharePoint objects. So, we will start with a simple example like populating the SharePoint sub sites in fabric Table component.

We are using PnP JS component for retrieving the sub-sites and fabric table components for rendering the retrieved results in a table structure.

Prerequisites - We have to include some references before using the PnP JS, and fabric components.

For PnP JS
  • Download PnP.js PnP JS file
  • Download fetch.js Used by PnP JS file to handle the web requests and responses (Required in IE)
  • Download es6-promise.js Used by PnP JS file to handle the web requests and responses (Required in IE)
For Fabric UI
Include the above files as a script and style references in the code and then, call the PnP JS methods to retrieve the sub sites from the current SharePoint site.

Note

Office UI Fabric CSS file path,

https://github.com/OfficeDev/office-ui-fabric-core//dist/css/fabric.min.css

Office UI Fabric JS file path,

https://github.com/OfficeDev/office-ui-fabric-js//dist/js/fabric.min.js
  1. <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>  
  2. <script type="text/javascript" src="/siteassets/scripts/es6-promise.js"></script>  
  3. <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>  
  4. <link href="/testdev/SiteAssets/Styles/fabric/fabric.min.css" rel="stylesheet" type="text/css">  
  5. <link href="/testdev/SiteAssets/Styles/fabric/fabric.components.min.css" rel="stylesheet" type="text/css">  
  6. <script type="text/javascript" src="/testdev/SiteAssets/Scripts/jquery.min.js"></script>  
  7. <script type="text/javascript" src="/testdev/SiteAssets/Scripts/fabric/fabric.min.js"></script>  
  8. <!-- Add HTML Content -->  
  9. <script type="text/javascript">  
  10.     //Fabric Component JS  
  11.     //PnP Code snippet  
  12. </script>  
Note - Include jQuery file used to enable the events for the components.

Example Snippets

In our example, we are using PnP JS component to retrieve all the sub sites from the SharePoint and generating the table rows with the retrieved sub site information and at last adding the row items to the Fabric Table component.

Generate Fabric Table Component

Replace the //Add HTML Content with the below HTML elements, 
  1. <div class="ms-font-xxl ms-bgColor-themeDarker ms-fontColor-themeLighterAlt">Sub Sites</div>  
  2. <table class="ms-Table ms-Table--selectable" id="webTable">  
  3.     <thead>  
  4.         <tr>  
  5.             <th class="ms-Table-rowCheck"></th>  
  6.             <th>Title</th>  
  7.             <th>Description</th>  
  8.             <th>Created</th>  
  9.             <th>Web Template</th>  
  10.         </tr>  
  11.     </thead>  
  12.     <tbody id="webTableBody"></tbody>  
  13. </table>  
  14. <p class="ms-font-m" id="countid">No websites</p>  
  15. </div>  
To enable the event handlers for the table component (like selecting the row and mouse hover effect to the table), use fabric JavaScript object to convert the normal HTML table to Fabric Table.
  1. //Convert the HTML table to Fabric Table Component  
  2. var TableElements = document.querySelectorAll(".ms-Table");  
  3. for(var i = 0; i < TableElements.length; i++) {  
  4. new fabric['Table'](TableElements[i]);  
  5. }  
The above code converts the normal HTML table component to the Fabric table component. This enables the hover effect on each row, row check selection.

Retrieve and populate sub sites

Add the below code by replacing the //PnP JS Code snippet. This will retrieve the sub sites from current SharePoint site using PnP JS component and in results inserting a rows to the fabric table.
  1. $pnp.sp.web.webs.get().then(function(result) {  
  2.     if (result.length > 0)  
  3.         document.getElementById('countid').innerHTML = "Total subsites: " + result.length;  
  4.     var tabhtml = "";  
  5.     for (var i = 0; i < result.length; i++) {  
  6.         var createdDate = new Date(result[i].Created);  
  7.         tabhtml += "<tr><td class='ms-Table-rowCheck'></td>" +  
  8.             "<td><a href='" + result[i].Url + "' target='_blank'>" + result[i].Title + "</a></td>" +  
  9.             "<td>" + result[i].Description + "</td>" +  
  10.             "<td>" + createdDate + "</td>" +  
  11.             "<td>" + result[i].WebTemplate + "</td>" + "</tr>";  
  12.     }  
  13.     document.getElementById("webTableBody").innerHTML = tabhtml;  
  14. }).catch(function(err) {  
  15.     alert(err);  
  16. });  
Full Code
 
Create a sample.html file and add all the above snippets. Then, link the file in content editor web part to run the snippet. Below is the concatenation of all the above snippets.
  1. //PnP JavaScript file available from https://github.com/OfficeDev/PnP-JS-Core  
  2. //Fabric Core CSS file available from https://github.com/OfficeDev/office-ui-fabric-core  
  3. //https://github.com/OfficeDev/office-ui-fabric-core/<Release build>/dist/css/fabric.min.css  
  4. //Fabric JS file available from https://github.com/OfficeDev/office-ui-fabric-js  
  5. //https://github.com/OfficeDev/office-ui-fabric-js/<Release build>/dist/js/fabric.min.js  
  6. <  
  7. script type = "text/javascript"  
  8. src = "/siteassets/scripts/fetch.js" > < /script>  <  
  9.     script type = "text/javascript"  
  10. src = "/siteassets/scripts/es6-promise.js" > < /script>  <  
  11.     script type = "text/javascript"  
  12. src = "/SiteAssets/Scripts/pnp.js" > < /script>  <  
  13.     link href = "/SiteAssets/Styles/fabric/fabric.min.css"  
  14. rel = "stylesheet"  
  15. type = "text/css" >  
  16.     <  
  17.     link href = "/SiteAssets/Styles/fabric/fabric.components.min.css"  
  18. rel = "stylesheet"  
  19. type = "text/css" >  
  20.     <  
  21.     script type = "text/javascript"  
  22. src = "/SiteAssets/Scripts/jquery.min.js" > < /script>  <  
  23.     script type = "text/javascript"  
  24. src = "/SiteAssets/Scripts/fabric/fabric.min.js" > < /script>  <  
  25.     div class = "ms-font-xxl ms-bgColor-themeDarker ms-fontColor-themeLighterAlt" > Sub Sites < /div> <  
  26.     table class = "ms-Table ms-Table--selectable"  
  27. id = "webTable" >  
  28.     <  
  29.     thead >  
  30.     <  
  31.     tr >  
  32.     <  
  33.     th class = "ms-Table-rowCheck" > < /th>  <  
  34.     th > Title < /th>  <  
  35.     th > Description < /th>  <  
  36.     th > Created < /th>  <  
  37.     th > Web Template < /th>  <  
  38.     /tr>  <  
  39.     /thead>  <  
  40.     tbody id = "webTableBody" > < /tbody> <  
  41.     /table>  <  
  42.     p class = "ms-font-m"  
  43. id = "countid" > No websites < /p> <  
  44.     /div> <  
  45.     script type = "text/javascript" >  
  46.     //Convert the HTML table to Fabric Table Component  
  47.     var TableElements = document.querySelectorAll(".ms-Table");  
  48. for (var i = 0; i < TableElements.length; i++) {  
  49.     new fabric['Table'](TableElements[i]);  
  50. }  
  51. //Retrieve the subsites and then populating the values in a Fabric Table  
  52. $pnp.sp.web.webs.get().then(function(result) {  
  53.     if (result.length > 0)  
  54.         document.getElementById('countid').innerHTML = "Total subsites: " + result.length;  
  55.     var tabhtml = "";  
  56.     for (var i = 0; i < result.length; i++) {  
  57.         var createdDate = new Date(result[i].Created);  
  58.         tabhtml += "<tr><td class='ms-Table-rowCheck'></td>" +  
  59.             "<td><a href='" + result[i].Url + "' target='_blank'>" + result[i].Title + "</a></td>" +  
  60.             "<td>" + result[i].Description + "</td>" +  
  61.             "<td>" + createdDate + "</td>" +  
  62.             "<td>" + result[i].WebTemplate + "</td>" + "</tr>";  
  63.     }  
  64.     document.getElementById("webTableBody").innerHTML = tabhtml;  
  65. }).catch(function(err) {  
  66.     alert(err);  
  67. }); <  
  68. /script>  
Please click here to view this article from my blog. The code snippets are also available in my GitHub page.
 
Output
Output
Conclusion

This article gives you the overview of building modern UI experience to the SharePoint, using Office Fabric UI component and PnP JS Library component.