Display Template Configuration To Display In Datagrid In SharePoint Search

In my earlier post "Custom Search Configuration In SharePoint Online", we created managed property and refiners which we later used to display search results.

Now we will see about modified control template and item template to display the search result in the data grid.

Each display template is made of two files- an HTML version of the display template, which you can edit in your HTML editor and a .js file which SharePoint converts from the HTML file.
HTML file 
Steps
  1. Navigate to Site Action -> Site Settings in SharePoint publishing site
  2. Click “Master pages and page layouts” under Web Design Galleries.
  3. Click Display Templates in master page gallery. -> Search
  4. For Control template, Click Control_List.html template file and download a copy and rename the file as Control_Grid.html
  5. For Item template, Click Item_Diagnostic.html template file and download a copy and rename the file as Item_Grid.html
Open Control_Grid.html file and put <Script> section under the body:
  1. <script>  
  2.     $includeScript(this.url, "https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.0.min.js");  
  3.     $includeScript(this.url, "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js");  
  4.     $includeCSS(this.url, "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css");  
  5.     $includeLanguageScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Language Files/{Locale}/CustomStrings.js");  
  6. </script>  
HTML file 

Now replace the <div id='Control_Grid'> with the following script.
  1. <div id="Control_Grid">  
  2.     <!--#_  
  3. if (!$isNull(ctx.ClientControl) &&  
  4. !$isNull(ctx.ClientControl.shouldRenderControl) &&  
  5. !ctx.ClientControl.shouldRenderControl())  
  6. {  
  7. return "";  
  8. }  
  9. ctx.ListDataJSONGroupsKey = "ResultTables";  
  10. var $noResults = Srch.ContentBySearch.getControlTemplateEncodedNoResultsMessage(ctx.ClientControl);  
  11. var noResultsClassName = "ms-srch-result-noResults";  
  12. //Declare Table and Header IDs  
  13. var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_Table_");  
  14. var headerRowId = $htmlEncode(encodedId + "_HeaderRow_");  
  15. //Post Render Events  
  16. ctx.OnPostRender = [];  
  17. ctx.OnPostRender.push(function () {  
  18. $("#" + encodedId).dataTable({ "bPaginate"false"bAutoWidth"true"bSort"false });  
  19. });  
  20. //It will be called for each row  
  21. var ListRenderRenderWrapper = function(itemRenderResult, inCtx, tpl)  
  22. {  
  23. var iStr = [];  
  24. iStr.push(itemRenderResult);  
  25. return iStr.join('');  
  26. }  
  27. ctx['ItemRenderWrapper'] = ListRenderRenderWrapper;  
  28. _#--> //Define container and headers <table id="_#= encodedId =#_" class="resultsTable">  
  29.         <thead>  
  30.             <tr id="_#= headerRowId =#_" class="resultsTableHeader">  
  31.                 <th>Product</th>  
  32.                 <th>ProductCode</th>  
  33.                 <th>Status</th>  
  34.             </tr>  
  35.         </thead> _#= ctx.RenderGroups(ctx) =#_  
  36.     </table>  
  37.     <!--#_  
  38. if (ctx.ClientControl.get_shouldShowNoResultMessage())  
  39. {  
  40. _#-->  
  41.     <div class="_#= noResultsClassName =#_">_#= $noResults =#_</div>  
  42.     <!--#_  
  43. }  
  44. _#-->  
  45.     <!-- Paging Display -->  
  46.     <!--#_  
  47. if(ctx.ClientControl.get_showPaging()){  
  48. var pagingInfo = ctx.ClientControl.get_pagingInfo();  
  49. var rowsPerPage = ctx.ClientControl.get_numberOfItems();  
  50. var totalRows = ctx.DataProvider.get_totalRows();  
  51. var currentPage = ctx.ClientControl.get_currentPageNumber();  
  52. var totalPages = Math.ceil(totalRows / rowsPerPage);  
  53. var selfLinkId = "SelfLink_";  
  54. var imagesUrl = GetThemedImageUrl('searchresultui.png');  
  55. var pl = '';  
  56. if(!$isEmptyArray(pagingInfo)){  
  57. _#-->  
  58.     <div class="MWS_Control_ListWithPaging_Paging_Wrapper col-xs-6 col-sm-12">  
  59.         <!--#_  
  60. for (var i = 0; i < pagingInfo.length; i++) {  
  61. var pl = pagingInfo[i];  
  62. if(!$isNull(pl)) {  
  63. var imagesUrl = GetThemedImageUrl('searchresultui.png');  
  64. if(pl.startItem == -1 && currentPage != totalPages) {  
  65. var selfLinkId = "SelfLink_" + pl.pageNumber;  
  66. _#-->  
  67.         <div id="PagingSelf">Page _#= $htmlEncode(pl.pageNumber) =#_</div>  
  68.         <!--#_  
  69. else if(pl.pageNumber == -1) {  
  70. var iconClass = Srch.U.isRTL() ? "ms-srch-pagingNext" : "ms-srch-pagingPrev";  
  71. _#-->  
  72.         <div id="PreviousImageLink">  
  73.             <a id="PageLinkPrev" href="#" class="ms-commandLink ms-promlink-button ms-promlink-button-enabled ms-verticalAlignMiddle" title="_#= $htmlEncode(pl.title) =#_" onclick="$getClientControl(this).page(_#= $htmlEncode(pl.startItem) =#_);return Srch.U.cancelEvent(event);">  
  74.                 <span class="ms-promlink-button-image">  
  75.                     <img src="_#= $urlHtmlEncode(imagesUrl) =#_" class="_#= $htmlEncode(iconClass) =#_" alt="_#= $htmlEncode(pl.title) =#_" />  
  76.                 </span>  
  77.             </a>  
  78.         </div>  
  79.         <!--#_  
  80. else if(pl.pageNumber == -2) {  
  81. var iconClass = Srch.U.isRTL() ? "ms-srch-pagingPrev" : "ms-srch-pagingNext";  
  82. //Do not display anything.  
  83. }  
  84. }  
  85. }  
  86. //Display the last page  
  87. if((currentPage != totalPages) && totalPages > 2) {  
  88. _#-->  
  89.         <div id="PagingLink">of _#= $htmlEncode(totalPages) =#_</div>  
  90.         <!--#_  
  91. }else if(currentPage == totalPages){  
  92. _#-->  
  93.         <div id="PagingLink">Page _#= $htmlEncode(totalPages) =#_ of _#= $htmlEncode(totalPages) =#_</div>  
  94.         <!--#_  
  95. }  
  96.   
  97. for (var i = 0; i < pagingInfo.length; i++) {  
  98. var pl = pagingInfo[i];  
  99. if(!$isNull(pl)) {  
  100. var imagesUrl = GetThemedImageUrl('searchresultui.png');  
  101. if(pl.pageNumber == -2) {  
  102. var iconClass = Srch.U.isRTL() ? "ms-srch-pagingPrev" : "ms-srch-pagingNext";  
  103. _#-->  
  104.         <div id="PagingImageLink">  
  105.             <a id="PageLinkNext" href="#" class="ms-commandLink ms-promlink-button ms-promlink-button-enabled ms-verticalAlignMiddle" title="_#= $htmlEncode(pl.title) =#_" onclick="$getClientControl(this).page(_#= $htmlEncode(pl.startItem) =#_);return Srch.U.cancelEvent(event);">  
  106.                 <span class="ms-promlink-button-image">  
  107.                     <img src="_#= $urlHtmlEncode(imagesUrl) =#_" class="_#= $htmlEncode(iconClass) =#_" alt="_#= $htmlEncode(pl.title) =#_" />  
  108.                 </span>  
  109.             </a>  
  110.         </div>  
  111.         <!--#_  
  112. }  
  113. }  
  114. }  
  115. _#-->  
  116.         <!-- Total item count. -->  
  117.         <!--#_  
  118. if(ctx.ClientControl.get_showResultCount() && ctx.DataProvider.get_totalRows() > 0){  
  119. var start = ctx.DataProvider.get_currentQueryState().s;  
  120. var resultsPerPage = ctx.DataProvider.get_resultsPerPage();  
  121. var totalRows = ctx.DataProvider.get_totalRows();  
  122. var countDisplayString = Srch.Res.rs_ApproximateResultCount;  
  123. var exactCount = Srch.Res.rs_ResultCount;  
  124. if (start + resultsPerPage > totalRows) { countDisplayString = (totalRows == 1)? Srch.Res.rs_SingleResultCount : Srch.Res.rs_ResultCount; }  
  125. _#-->  
  126.         <div id="ResultCount" class="ms-srch-resultscount"> _#= String.format($htmlEncode(exactCount), $htmlEncode(totalRows.localeFormat("N0"))) =#_ </div>  
  127.         <!--#_  
  128. }  
  129. _#-->  
  130.     </div> <!-- //Close MWS_Control_ListWithPaging_Paging_Wrapper -->  
  131.     <!--#_  
  132. }  
  133. }  
  134. _#-->  
  135. </div>//Save and close file  
I have also included pagination in this datagrid view.

Now its time to update item_Grid.html file.

Step 1

Open the file and look for <mso:ManagedPropertyMapping in the head section. Replace the line with the following,
  1. <mso:ManagedPropertyMapping msdt:dt="string">'SLProduct':'SLProduct','SLProductCode':'SLProductCode','SLProductStatus':'SLProductStatus','Path':'Path'</mso:ManagedPropertyMapping>  
Note
You need to update with your managed property name in the above line which you created while configuring search results.

Step 2

Replace the whole body script with the below,
  1. <body>  
  2.     <div id="Item_GridRow">  
  3.         <!--#_  
  4. var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_Diagnostic_");  
  5. var linkURL = $getItemValue(ctx, "Path");  
  6. linkURL.overrideValueRenderer($urlHtmlEncodeValueObject);  
  7. var SLProduct = $getItemValue(ctx, "SLProduct");  
  8. var SLProductCode = $getItemValue(ctx, "SLProductCode");  
  9. var SLProductStatus = $getItemValue(ctx, "SLProductStatus");  
  10. if(SLProduct==''){  
  11. SLProduct='No Value';  
  12. }  
  13.   
  14. _#-->  
  15.         <tr class="gridRow">  
  16.             <td><a href="_#= linkURL =#_" target="_blank">_#= $htmlEncode(SLProduct) =#_</a></td>  
  17.             <td>_#= $htmlEncode(SLProductCode) =#_</td>  
  18.             <td>_#= $htmlEncode(SLProductStatus) =#_</td>  
  19.         </tr>  
  20.     </div>  
  21. </body> 
Now you can upload the templates and publish as a major version.

Add content search web parts and edit the web part to configure result source under change query. Change control and item under display template with our title name.

Here title of control template and item template will be visible which we have given in our Control_Grid.html and Item_Grid.html respectively.
control template 
Leave another field as default - Apply -Ok.

Your result will be visible in the data grid as shown above.

I have attached both files with this blog. You can download and update with your managed metadata and start using it. 

Cheers!!