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.
Steps
- Navigate to Site Action -> Site Settings in SharePoint publishing site
- Click “Master pages and page layouts” under Web Design Galleries.
- Click Display Templates in master page gallery. -> Search
- For Control template, Click Control_List.html template file and download a copy and rename the file as Control_Grid.html
- 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:
- <script>
- $includeScript(this.url, "https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.0.min.js");
- $includeScript(this.url, "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js");
- $includeCSS(this.url, "https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css");
- $includeLanguageScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Language Files/{Locale}/CustomStrings.js");
- </script>
Now replace the <div id='Control_Grid'> with the following script.
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,
- <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,
- <body>
- <div id="Item_GridRow">
- <!--#_
- var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_Diagnostic_");
- var linkURL = $getItemValue(ctx, "Path");
- linkURL.overrideValueRenderer($urlHtmlEncodeValueObject);
- var SLProduct = $getItemValue(ctx, "SLProduct");
- var SLProductCode = $getItemValue(ctx, "SLProductCode");
- var SLProductStatus = $getItemValue(ctx, "SLProductStatus");
- if(SLProduct==''){
- SLProduct='No Value';
- }
-
- _#-->
- <tr class="gridRow">
- <td><a href="_#= linkURL =#_" target="_blank">_#= $htmlEncode(SLProduct) =#_</a></td>
- <td>_#= $htmlEncode(SLProductCode) =#_</td>
- <td>_#= $htmlEncode(SLProductStatus) =#_</td>
- </tr>
- </div>
- </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.
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!!