This is a blog post in the series "How to change how search results are displayed." To demonstrate how you can customize your search results, I'll use examples from an internal Microsoft Search Center.
For an overview of the blog post in this series, go to How to change the way search results are displayed in SharePoint Server 2013.
In the previous blog post, I showed you a simple method for adding a custom icon and values from two custom managed properties to your search results. In this blog post, I'll show you a somewhat more complete method for changing the way your search results are displayed that includes if statements and hit highlighting. We’ll learn:
- Strategy for killing three birds with one stone – search results version
- How to display values from custom managed properties with hit highlighting, and get automatically improved relevancy
First, let’s state what we want to achieve:
- Display values from two custom managed properties
- Apply hit highlighting to the two custom managed properties
- Get automatically improved relevancy for our search results
Before I show you how to do this, let's look at the strategy we want to follow to accomplish this. If it gets a bit complicated, please try to hang in there. Hopefully it'll be clear by the end of this blog.
First, remember how we can think about hit highlighting:
- The managed properties that are listed in the Hit-highlighted properties (JSON) section of the Search Results Web Part and the "magical summary" property are passed to the HitHighlightedProperties property.
- All values of the HitHighlightedProperties property are passed to the HitHighlightedSummary property.
- A truncated version of the values in HitHighlightedSummary is displayed in the Search Results Web Part with three dots at the end.
Also remember that each item display template contains a reference to the Item_CommonItem_Body display template, and that this template contains an onlick method that will result in automatically improved relevance based on the end-user’s click behavior.
So our strategy is simply this: create variables in the item display template that will be passed on and rendered by the Item_CommonItem_Body display template.
Specifically, that means that we have to do the following:
- Add the custom managed properties that we want to display in our search results to the Hit-highlighted properties in the Search Results Web Part.
- Add the custom managed properties to an item display template.
- In the item display template, create a new variable that will be used by the property HitHighlightedSummary to display our two custom managed properties with hit highlighting.
- In the item display template, leave the reference _#=ctx.RenderBody(ctx)=#_ so that the Item_ComonItem_Body display template will render the search result. This ensures that we get automatically improved relevancy.
OK, now let’s take it step-by-step, with examples of how I did this for my scenario.
First you have to find the managed property names that correspond to the custom site columns that you want to use. I showed you how to do this in a previous blog.
Then you have to do some configuration on the Search Results Web Part. Here's what you should do:
- On the Search results page, choose the Settings menu, and then choose Edit Page.
- In the Search Results Web Part, choose Web Part Menu –> Edit Web Part.
- In the Web Part tool pane, choose to expand the Display Templates section and then select Use a single template to display items. This will allow you to make changes to the Hit-highlighted properties (JSON) field.
- In the Hit-highlighted properties (JSON) field, use the following format to add the custom managed properties that you want to add hit highlighting to:
"<Managed property name>"
In my scenario, I wanted to apply hit highlighting to the ContentSummaryOWSMTXT and the owstaxIdTechnicalSubject managed properties.
- Choose Apply to save the changes. This will close the Display Templates section.
- Choose Display Templates to reopen the section, and choose Use result types to display items.
- Choose OK and save the page.
Then you need to add the custom managed properties to an item display template. Here’s what you should do:
- Open the item display template that belongs to the result type for which you want to customize search results.
In my scenario, this was TechNet content.
- In the item display template, in the ManagedPropertyMapping tag, use the following syntax to add the custom managed properties that you want to display:
'<Current item property name>':<Managed property name>'
In my scenario, I wanted the values from the managed properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject to be displayed in the search result. To make the file easier to maintain, I named the current item properties the same as the managed properties.
Then you need to create variables in the item display template that will be used and rendered by the Item_Common_Item_Body display template. Here's what you should do:
- Because you have no guarantee that the values of your custom properties will contain any of the entered query words, that is, hit highlighting will not be used, you have to create variables that ensure that the value of your custom properties will be displayed regardless of hit highlighting.
The following screenshots show how I created two such variables for my custom properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject.
- In addition, I added a similar variable for the Title property. If you don’t add this, the search results will not be rendered.
- The last step you have to do in the item display template is to create a variable that will override the HitHighlightedSummary property used to display the values.
- Save the item display template.
- NOTE: You do not need to do this step if you are using SharePoint Online.
Go to Site settings –> Search Result Types. Notice that a Property Sync alert is displayed.
This alert is displayed because we added managed properties to an item display template (what we did in step 9). To update the result types with the newly added managed properties, choose Update.
IMPORTANT! If you don't do this update, the newly added managed properties will not display in your search results.
After I made these changes, when users entered a query in the Search Center, the search result included:
- A custom icon.
- The value of Title with hit highlighting.
- The value of ContentSummaryOWSMTXT with hit highlighting.
- The value of owstaxIdTechnicalSubject. The query words did not match the property value, however because of the variable that I created in step 10, the value is displayed.
- A link to the item in the list
I wanted to make one little change to how the value for owstaxIdTechnicalSubject was displayed. I wanted to give users a bit more context as to what this value represents, so I decided to add the text "Technical Subject:" before the value. Also, because this value is not always present for all list items, I decided that it should only display when a value was present.
To do this, I made a change to the variable that overrides the HitHighlightedSummary property:
Notice that I added a slightly different color to the text "Technical Subject:". With this addition, the final search result is displayed like this:
In our planning, we had decided that we wanted 6 different result types. After creating the TechNet content result type and display template, it was very easy to copy this work over to the other 5 result types.
And here’s the result:
So now that we have changed the way search results are displayed, the next step is to change the values that are displayed in the hover panel.
Next blog post in this series
How to display values from custom managed properties in the hover panel