KnockoutJS in SharePoint 2013

Introduction

KnockoutJS is a JavaScript library for binding data and generating HTML at run time. It uses the Model-View-ViewModel pattern. To learn more about KnockoutJS please refer to http://learn.knockoutjs.com/#/?tutorial=intro.

In this article you will see how to use KnockoutJS in a SharePoint 2013 page. I have created a custom list named “Employee Details” that contains the following fields.



We will create a page and populate the preceding list items in a table using KnockoutJS and the JavaScript object model. We need to refer to the following JavaScript files:

  1. ko.sp-1.0.min.js
  2. knockout-3.1.0.js
  3. jquery-1.8.3.min.js

Create a page

  1. Navigate to the SharePoint site.
  2. Click on Settings and then click on Add a page.



  3. Enter the page name and then click on the Create button.



  4. The page is created successfully.

Add Script Editor Webpart

  1. Navigate to the newly created page.
  2. Click on the Page tab and then click on the Edit button in the ribbon interface.



  3. Click on the Insert tab and then click on Web Part.



  4. Select the Script Editor web part and then click on the Add button.



  5. Edit the web part and then click on Edit Snippet.



  6. Copy and paste the following code snippet and then click on the Insert button.
    1. <script src="https://c986.sharepoint.com/SiteAssets/jquery-1.8.3.min.js"></script>  
    2. <script src="https://c986.sharepoint.com/SiteAssets/knockout-3.1.0.js"></script>  
    3. <script src="https://c986.sharepoint.com/SiteAssets/ko.sp-1.0.min.js"></script>  
    4. <script>  
    5.   
    6.     ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js");  
    7.     var completeEmployeeList = null;  
    8.   
    9.     // Class used for saving the field values.  
    10.     function EmployeeList(name, designation, department, location) {  
    11.         var self = this;  
    12.         self.Name = name;  
    13.         self.Designation = designation;  
    14.         self.Department = department;  
    15.         self.Location = location;  
    16.     }  
    17.   
    18.     //  View Model - JavaScript that defines the data and behavior of your UI  
    19.     function EmployeeListViewModel() {  
    20.         var self = this;  
    21.         // observableArray equivalent of a regular array,  
    22.         self.Employees = ko.observableArray([]);  
    23.         self.AddEmployees = function (name, designation, department, location) {  
    24.             self.Employees.push(new EmployeeList(name, designation, department, location));  
    25.         }  
    26.     }  
    27.   
    28.     function MainFunction() {  
    29.         completeEmployeeList = new EmployeeListViewModel();  
    30.   
    31.         // Retrieve the SharePoint list items  
    32.         retrieveListItems();  
    33.   
    34.         // Activates knockout.js  
    35.         ko.applyBindings(completeEmployeeList);  
    36.     }  
    37.   
    38.     function retrieveListItems() {  
    39.         var clientContext = new SP.ClientContext.get_current();  
    40.         var oList = clientContext.get_web().get_lists().getByTitle('Employee Details');  
    41.         var camlQuery = new SP.CamlQuery();  
    42.         camlQuery.set_viewXml("<View><RowLimit>10</RowLimit></View>");  
    43.         this.collListItem = oList.getItems(camlQuery);  
    44.         clientContext.load(collListItem);  
    45.         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));  
    46.     }  
    47.   
    48.     function onQuerySucceeded(sender, args) {  
    49.         var listItemInfo = '';  
    50.         var listItemEnumerator = collListItem.getEnumerator();  
    51.         while (listItemEnumerator.moveNext()) {  
    52.             var currentItem = listItemEnumerator.get_current();  
    53.             completeEmployeeList.AddEmployees(currentItem.get_item("Title"), currentItem.get_item("Designation"), currentItem.get_item("Department"), currentItem.get_item("Location"));  
    54.         }  
    55.     }  
    56.   
    57.     function onQueryFailed(sender, args) {  
    58.         alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
    59.     }  
    60. </script>  
    61.   
    62. <!-- view - HTML markup that defines the appearance of your UI -->  
    63. <div id="divEmployeeList">  
    64.   
    65.     <h2>Employee Details</h2>  
    66.     <br />  
    67.   
    68.     <table id="tblEmployeeList" border="1">  
    69.         <thead>  
    70.             <tr>  
    71.                 <th>Name</th>  
    72.                 <th>Designation</th>  
    73.                 <th>Department</th>  
    74.                 <th>Location</th>  
    75.             </tr>  
    76.         </thead>  
    77.         <!-- Iterating through every list item using foreach of KO -->  
    78.         <tbody data-bind="foreach: Employees">  
    79.             <tr>  
    80.                 <td data-bind="text: Name"></td>  
    81.                 <td data-bind="text: Designation"></td>  
    82.                 <td data-bind="text: Department"></td>  
    83.                 <td data-bind="text: Location"></td>  
    84.             </tr>  
    85.         </tbody>  
    86.     </table>  
    87. </div> 
  7. Click on the Ok button in the web part properties window.
  8. Click on the Save button in the ribbon interface.
  9. You are able to see the following HTML table populated with all the list items.


Summary

Thus in this article you saw how to use KnockoutJS in a SharePoint 2013 page.