Read Data Using Client Side JavaScript Object Model and Apply Search Using jQuery DataTable

Create a List called POC containing the following columns (Technology and Customer),

columns

Once the list is created, enter some data into the list.

Now we will create an HTML File called POC.html which will contain the design view of our project.

  1. <html>  
  2. <head>  
  3. <!--incude knockout files-->  
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>  
  5. <!--knockout script ends here-->  
  6. <!--include sharepoint javascript api-->  
  7. <script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>  
  8. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  9. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  10. <!--sharepoint api ends here-->  
  11. <!--jquery dataTables api starts here-->  
  12. <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>  
  13. <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>  
  14. <!--jquery datatable ends here-->  
  15. <!--Jquery datatable css-->  
  16. <link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">  
  17. <script type="text/javascript" src=”/_catalogs/masterpage/POC/Scripts/POC.js"></script>  
  18. </head>  
  19. <body>  
  20. <table  id="POCtable">    
  21.     <thead>    
  22.         <tr>    
  23.             <th>Title</th>    
  24.            <th>Column1</th>    
  25.             <th>Column2</th>    
  26.         </tr>    
  27.     </thead>    
  28.     <tbody data-bind="foreach: assignments">    
  29.        
  30. <tr>  
  31.         <td><span data-bind="text: Title"></span></td>  
  32.         <td><span data-bind="text: Column1"></span></td>  
  33.         <td><span data-bind="text: Column2"></span></td>  
  34.         </tr>  
  35.           
  36.     </tbody>    
  37.     </body>  
  38. </table></html>  
Now we will create a POC.js file which will have the knockout binding,
  1. 'use strict';  
  2. var listTitle = "POC";  
  3. var appWebContext;  
  4. var hostweburl;  
  5.   
  6. $(document).ready(function () {  
  7.     ViewGrid();  
  8.   
  9. });  
  10.   
  11. function ViewGrid() {  
  12.     ko.applyBindings(new TestListViewModel());  
  13.   
  14.   
  15. }  
  16.   
  17. function TestListViewModel() {  
  18.     var self = this;  
  19.     appWebContext = new SP.ClientContext.get_current();  
  20.     self.assignments = ko.observableArray();  
  21.     self._loadList = function () {  
  22.         var clientContext = SP.ClientContext.get_current();  
  23.         var list = appWebContext.get_web().get_lists().getByTitle('POC');  
  24.         var query = new SP.CamlQuery();  
  25.         query.set_viewXml("<View>" + " <Query>" + " <OrderBy>" + "<FieldRef Name='Modified' Ascending='True' />" + " </OrderBy>" + " </Query>" + " <ViewFields>" + "<FieldRef Name='Title' /><FieldRef Name='mj8o' /><FieldRef Name='p4zw' />" + " </ViewFields>" + "</View>");  
  26.         self._pendingItems = list.getItems(query);  
  27.         clientContext.load(self._pendingItems);  
  28.         clientContext.executeQueryAsync(Function.createDelegate(self, self._onLoadListSucceeded), Function.createDelegate(self, self._onLoadListFailed));  
  29.     }  
  30.     self._onLoadListSucceeded = function (sender, args) {  
  31.         var listEnumerator = self._pendingItems.getEnumerator();  
  32.         while (listEnumerator.moveNext()) {  
  33.             var item = listEnumerator.get_current().get_fieldValues();  
  34.             self.assignments.push(  
  35.             {  
  36.                 Title: item.Title.toString(),  
  37.                 Column1: item.mj8o.toString(), //column1 name  
  38.                 Column2: item.p4zw.toString(), //column2 name  
  39.   
  40.             });  
  41.         }  
  42.         $("#POCtable").DataTable();     //bind jquery datatable by using .DataTable method        
  43.     }  
  44.     self._onLoadListFailed = function (sender, args) {  
  45.         alert('Unable to load file list: ' + args.get_message() + '\n' + args.get_stackTrace());  
  46.     }  
  47.     self._loadList();  
  48. }  
Now upload these files in the following location “_catalogs/masterpage/” by creating a folder called POC.

In this folder create two subfolders, HTML and Scripts, and add the files respectively,
folder
Now create a page, add a content editor Web part, add the url and see the magic:

page

You can search , do paging and select the number of entries.

page
Ebook Download
View all
Learn
View all