From my previous posts, we can learn how to integrate PnP JavaScript library in Angular JS 1.0 and React JS frameworks, from the following links.
In this article, I am taking the same example but a different JS framework to build the UI interfaces.
In the modern SharePoint experience, the SharePoint team uses Knockout JS as one of the frameworks to update the UI interfaces.
Knockout JS supports MVVM pattern (Model – View – ViewModel) to build the web UI interface and it follows a different model in updating DOM as compared to the other JS frameworks. You can learn more about Knockout JS from their site. They are providing nice examples and tutorials to learn it step by step (I also have learned from those tutorials).
PnP-JS-Core library contains the number of extensible methods and properties. By using that, we can achieve the various actions in a simple code. To know more about this library component, visit the below links,
After reading the above links, you will have the basic idea about PnP-JS-Core library and how to use it in accessing the SharePoint.
Now, we will get back on the integration part. Before starting it, we have to identify some required files for this example.
What essential files are required?
- knockoutjs,js A latest Knockout java script library for building user interfaces
- Pnp.js PnP JavaScript library
- Fetch.js Used by PnP js file to handle the web requests and responses (Required for IE)
- Promise.js Used by PnP js file to handle the web requests and responses (Required for IE)
Note: Fetch 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 the Site Assets library and added the below lines to our sample html file.
- <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/knockout-3.4.0.js"></script>
Note: I have downloaded the latest knockout js file and referenced it in the script.
Include HTML
Add the below css styles and html snippet to the sample html file.
-
- <style type="text/css">
- .web-table th{ background-color:#ddd; border:2px solid #fff;}
- .web-table td{ background-color:#eee; border:2px solid #fff;}
- .web-heading{ padding:2px;}
- </style>
- <div>
- <h2 class="web-heading">Sub Sites</h2>
- <div>
- <table width="100%" cellpadding="10" cellspacing="2" class="web-table">
- <thead>
- <tr>
- <th>Title</th>
- <th>Id</th>
- <th>Created</th>
- <th>Web Template</th>
- </tr>
- </thead>
- <tbody data-bind="foreach: webSites">
- <tr >
- <td data-bind="text: Title"></td>
- <td data-bind="text: Id"></td>
- <td data-bind="text: Created"></td>
- <td data-bind="text: WebTemplate"></td>
- </tr>
- </tbody>
- </table>
- <p data-bind="visible: webSites().length <= 0">No websites</p>
- </div>
- </div>
-
- <script type="text/javascript">
- <!- - Insert Knockout JS Code - - >
- </script>
- Data-bound attributes are how Knockout lets you declaratively associate viewmodel properties with DOM elements. You just use the text binding to assign the text to your DOM elements.
- Websites represent observable object – this is a property that, automatically, will issue notifications whenever their value changes.
- If sub sites are not available, “No websites” content is visible to the user by using visible binding and by comparing the length property of a observable property.
Include Knockout JS Code
- // This is a simple viewmodel - JavaScript that defines the data and behavior of your UI
- function websViewModel() {
- var self = this;
-
- self.webSites = ko.observableArray();
-
- //Include PnP JS Code to set the subsite collection to self.webSites variable
- }
-
- // Activates knockout.js
- ko.applyBindings(new websViewModel());
The ko.applyBindings method activates the knockout code on websViewModel. And, the properties and methods are executed to store the variables in a viewModel property.
Include PnP JS Code
Replace the included PnP JS Code comment from the html snippet, with the below PnP code.
- $pnp.sp.web.webs.get().then(function(result) {
- self.webSites(result); //knockout JS Code used to set the result object to webSites
- });
PnP executes the code and sets the returned value to the website's observable property. We can use this observable property in html to update the content in the UI.
Full Sample Code
Insert the below code in Script Editor or Content Editor web-part.
- <style type="text/css">
- .web-table th{ background-color:#ddd; border:2px solid #fff;}
- .web-table td{ background-color:#eee; border:2px solid #fff;}
- .web-heading{ padding:2px;}
- </style>
- <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>
- <script type="text/javascript" src="/siteassets/scripts/knockout-3.4.0.js"></script>
-
- <div>
- <h2 class="web-heading">Sub Sites</h2>
- <div>
- <table width="100%" cellpadding="10" cellspacing="2" class="web-table">
- <thead>
- <tr>
- <th>Title</th>
- <th>Id</th>
- <th>Created</th>
- <th>Web Template</th>
- </tr>
- </thead>
- <tbody data-bind="foreach: webSites">
- <tr >
- <td data-bind="text: Title"></td>
- <td data-bind="text: Id"></td>
- <td data-bind="text: Created"></td>
- <td data-bind="text: WebTemplate"></td>
- </tr>
- </tbody>
- </table>
- <p data-bind="visible: webSites().length <= 0">No websites</p>
- </div>
- </div>
-
- <script type="text/javascript">
- // This is a simple viewmodel (websViewModel) - JavaScript that defines the data and behavior of your UI
- function websViewModel() {
- var self = this;
-
- self.webSites = ko.observableArray();
- $pnp.sp.web.webs.get().then(function(result) {
- //knockout JS Code used to set the result object to webSites
- self.webSites(result);
- });
- }
- // Activates knockout.js
- ko.applyBindings(new websViewModel());
- </script>
Output
Conclusion
This article provides a simple example to list the sub sites in a table format using Knockout JS framework. Click
here to read the same article from my blog.