Applying Hit-Highlighting to any property in the Search Results WebPart in SharePoint 2013

Applies to: SharePoint 2013

 

In the beginning I thought this wasn’t possible without adding custom JavaScript code to the Display Template.

However, after some digging, this task seems to be a relatively simple one that can be solved by some minor configuration changes.

Follow the steps below to enable hit-highlighting of the value of any property that is returned by search results:

1. Edit the Search Results WebPart.

2. Under Display Templates, select “Use a single template to display items”. This is not what we want… but hold on.

3. The Hit-Highlighted properties (JSON) textbox is now editable, add the MP you want hit highlighting for in there as shown in the screenshot below.

Note: the Managed Property you add in there has to be configured as "retrievable" in your Search Schema, and added to the list of properties to be retrieved in your Display Template.

 4. Click on Apply – this will save the WebPart configuration.

5. Now back to the Display Templates section, select “Use result types to display items” to make the Search Results WebPart use the Result Type configuration to select which Display Template to apply for each result item.

 

6. Click on Apply or OK.

7. Save the Page to stop editing.

 

 Now, we'll need to customize our Display Template to extract the hit-highlighted value and display that value to the user.

1. Edit the Display Template; I am going to use the Item_PowerPoint.html template as an example. This file is located at: "https://site/_catalogs/masterpage/Display%20Templates/Search/Item_PowerPoint.html"

2. Use the section below of JavaScript code to get the hit-highlighted value of the required Managed Property. Here in my example, I have configured the "Parent Link" Managed Property to be hit-highlighted.

...

/* start: getting value of hit-highlighted property */

/* use this snippet to extract the value of a hithighlighted property */

/* get the current item id */

var csr_id = ctx.CurrentItem.csr_id;

/* get the hit-highlighted value */

var parentLinkHitHighlighted = Srch.U.getHighlightedProperty(csr_id, ctx.CurrentItem, "ParentLink");

/* get the original value in case nothing was returned in the previous */

var parentLinkUrl = ctx.CurrentItem.ParentLink;

if ($isEmptyString(parentLinkHitHighlighted)) {parentLinkHitHighlighted = $htmlEncode(ctx.CurrentItem.ParentLink);}

/* end: getting value of hit-highlighted property */

...

 

3. Finally, we add some HTML markup to render the "Parent Link" hit-highlighted value.

<!-- custom HTML markup to render the parent link -->

<div>

<span>In Library: </span><a href="_#=parentLinkUrl=#_">_#=parentLinkHitHighlighted=#_</a>

</div>

<!-- end custom markup -->

 

The final result can be seen here. I search for "sharepoint document" and you can see how my Parent Link property gets highlighted accordingly.

 

 

And below you can download the full version of the Item_PowerPoint.html Display Template used which includes the code snippets mentioned in this post.

Item_PowerPoint_custom.html