Bind SharePoint List Data Into JQuery Datatable Using Content Search Web Part (CSWP) In SharePoint Server 2013

In the first step, let's create the Authoring and Publishing Site Collections. Authoring Site collection allows authors to manage their website content while the Publishing Site collection reviews the content from the authoring site collection.

Open the Authoring Site Collection.

Create a list named “Employee Details” that contains Employee name, Employee designation, Employee ID, Employee designation, Blood group, Email, and Mobile.




Let’s go and create a control and a template to display the items.

  • Control Template

    It provides the overall structure (layout) of the HTML elements along with start and end tags.

  • Item Template

    It renders the items from the list, and contains text and pictures.

Now, let's create two display templates for displaying a bootstrap carousel menu control and a menu item.

Code – Datatable Control Template

Upload the script and CSS into style library.


Add the scripts and CSS into Control template.

  1. <script>  
  2.    $includeScript(this.url, "~sitecollection/Style%20Library/datatable/jquery.dataTables.min.css");  
  3.    $includeCSS(this.url, "~sitecollection/Style%20Library/datatable/jquery.dataTables.min.js");          
  4. </script>   

Now, let's define the HTML layout to the Control template.

  1. <table id="datatable" class="display">  
  2.     <thead>  
  3.         <tr>  
  4.             <th>Employee ID</th>  
  5.             <th>Employee Name</th>  
  6.     <th>Designation</th>  
  7.             <th>Blood Group</th>  
  8.     <th>Email</th>  
  9.             <th>Mobile</th>  
  10.         </tr>  
  11.     </thead>  
  12.     <tbody>  
  13.               
  14.     _#= ctx.RenderItems(ctx) =#_  
  15.                             
  16. </tbody>  
  17. </table>   

The full code looks like the following. 

  1. <html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">   
  2.     <head>  
  3.         <title>Datatable Control</title>  
  4.   
  5.         <!--[if gte mso 9]><xml>  
  6.         <mso:CustomDocumentProperties>  
  7.         <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  8.         <mso:MasterPageDescription msdt:dt="string">Display a Accordion Control</mso:MasterPageDescription>  
  9.         <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106601</mso:ContentTypeId>  
  10.         <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  11.         <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  12.         <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  13.         <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  14.         <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  15.         <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/bannercontrol.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  16. </mso:CustomDocumentProperties>  
  17.         </xml><![endif]-->  
  18.     </head>  
  19.   
  20.     <body>          
  21.         <script>  
  22.              $includeCSS(this.url, "~sitecollection/Style%20Library/datatable/jquery.dataTables.min.css");  
  23.               $includeScript(this.url, "~sitecollection/Style%20Library/datatable/jquery.dataTables.min.js");  
  24.             $includeScript(this.url, "~sitecollection/Style%20Library/datatable/jquery-1.8.2.min.js");  
  25.               
  26.               
  27.         </script>  
  28.   
  29.         <div>  
  30.             <!--#_if (!$isNull(ctx.ClientControl) && !$isNull(ctx.ClientControl.shouldRenderControl) && !ctx.ClientControl.shouldRenderControl()){return "";}  
  31.             ctx.ListDataJSONGroupsKey = "ResultTables";  
  32.             ctx["CurrentItems"] = ctx.ListData.ResultTables[0].ResultRows;  
  33.             var siteURL = SP.PageContextInfo.get_siteAbsoluteUrl();       
  34.               
  35.             AddPostRenderCallback(ctx, function() {  
  36.                 $.getScript(siteURL + "");  
  37.                   
  38.   $('#datatable').dataTable();  
  39.   
  40.                   
  41.                   
  42.     });   
  43.             _#-->  
  44. <table id="datatable" class="display">  
  45.     <thead>  
  46.         <tr>  
  47.             <th>Employee ID</th>  
  48.             <th>Employee Name</th>  
  49.             <th>Designation</th>  
  50.             <th>Blood Group</th>  
  51.             <th>Email</th>  
  52.             <th>Mobile</th>  
  53.         </tr>  
  54.     </thead>  
  55.     <tbody>  
  56.               
  57.     _#= ctx.RenderItems(ctx) =#_  
  58.                             
  59. </tbody>  
  60. </table>  
  61.   
  62. </div>  
  63.     </body>  
  64. </html>      

Datatable Item Template

Let’s go to Item template and declare the managed properties and necessary variables.

  1. <!--#_  
  2.             var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  3.             var itemIdx = ctx.CurrentItemIdx+1;  
  4.             var title = $getItemValue(ctx, "Title");   
  5.             var empid = $getItemValue(ctx, "Employee ID");  
  6.             var empname = $getItemValue(ctx, "Employee Name");  
  7.             var designation = $getItemValue(ctx, "Employee Designation");  
  8.             var blood = $getItemValue(ctx, "Blood Group");  
  9.             var email = $getItemValue(ctx, "Email);  
  10.             var mobile = $getItemValue(ctx, "Mobile);  
  11.             var esActive ="";  
  12.                       
  13.             _#-->   

Render the HTML structure with the list item information.

  1. <!--- HTML Goes Here -->  
  2.           
  3.          <tr>  
  4.             <td>_#= empid =#_</td>  
  5.             <td>_#= title =#_</td>  
  6.             <td>_#= designation =#_</td>  
  7.             <td>_#= blood =#_</td>  
  8.             <td>_#= email =#_</td>  
  9.             <td>_#= mobile =#_</td>  
  10.             </tr>   

Overall, the Item template code looks like the following. 

  1. <html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">   
  2.     <head>  
  3.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  4.         <title>Datatable Item</title>  
  5.         <!--[if gte mso 9]><xml>  
  6.         <mso:CustomDocumentProperties>  
  7.         <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  8.         <mso:MasterPageDescription msdt:dt="string">Displays an Datatable Item.</mso:MasterPageDescription>  
  9.         <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>  
  10.         <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  11.         <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  12.         <mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','Employee ID':'Employee ID','Employee Name':'Employee Name','Employee Designation':'Employee Designation','Blood Group':'Blood Group','Email':'Email','Mobile':'Mobile'</mso:ManagedPropertyMapping>  
  13.         <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  14.         <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  15.         <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  16.           
  17.         <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/BannerItem.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  18. </mso:CustomDocumentProperties>  
  19.         </xml><![endif]-->  
  20.     </head>  
  21.   
  22.               
  23.     <body>  
  24.         <div>  
  25.             <!--#_  
  26.             var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  27.             var itemIdx = ctx.CurrentItemIdx+1;  
  28.             var title = $getItemValue(ctx, "Title");   
  29.             var empid = $getItemValue(ctx, "Employee ID");  
  30.             var empname = $getItemValue(ctx, "Employee Name");  
  31.             var designation = $getItemValue(ctx, "Employee Designation");  
  32.             var blood = $getItemValue(ctx, "Blood Group");  
  33.             var email = $getItemValue(ctx, "Email");  
  34.             var mobile = $getItemValue(ctx, "Mobile");  
  35.             var esActive ="";  
  36.                       
  37.             _#-->  
  38.               
  39.         <!--- HTML Goes Here -->  
  40.           
  41.          <tr>  
  42.             <td>_#= empid =#_</td>  
  43.             <td>_#= title =#_</td>  
  44.             <td>_#= designation =#_</td>  
  45.             <td>_#= blood =#_</td>  
  46.             <td>_#= email =#_</td>  
  47.             <td>_#= mobile =#_</td>  
  48.             </tr>  
  49.                   
  50.           </div>  
  51.                   
  52.               
  53.           
  54.     </body>  
  55. </html>   

Upload the display template under Site Settings - > Master page and page layouts -> Display Templates -> Content Web Parts.


Now, we are going to insert some data into Employee details list from Authoring Site Collection.


Run the Search Service application "Crawl" now.


Now, let’s go to the SharePoint page.

Add a CSWP into this page.


Map a search query to get the result from the Authoring Site Collection.

Click Edit Web Part -> change query.

URL - http://technologygeeks/sites/authoring/Lists/Employee%20details/


Provide the number of items to display.


Choose the display templates.


Click OK. Under property mapping section, map the managed properties of the Employee name, Employee designation, Employee ID, Blood group, Email, and Mobile.


Click "OK" to complete the setup.

The following is the final result.


Provide Employee name for search.


Note  - The result has been displayed using Search Service. You need to run the Search Service application.

After adding the content into the SharePoint list, the result will be successfully displayed into the page.

Datatable Reference

https://datatables.net/