SharePoint – Customising Search Result Web Part Display Template

Ok, so following on from my previous post (here) I am still playing with display templates. My next target is the search results page.  What I wanted to achieve was a way of quickly searching a knowledge base that was built on the Enterprise Wiki site template.  The ultimate goal was to be able to enter a search term and then get the pages back, in full, that matched the query.  I’ve taken some of the formatting from my previous post so you will see some similarities between them.

Let’s start

There are quite a few differences between the CSWP and the Search Results web parts.  Lets have a quick look at what these are.

CSWP overview

The web part has 3 display templates all working together to product a single page.

1. “Control” display template that determines the look and feel of the body of the page along features like paginating etc.
2. “Item” display template controls how the search results are displayed
3. “Hover” display template controls what is shown on the hover panel.

I will stress at this part of the blog is that I am only focusing on what needs to be done to achieve my goal for this site.  There are many many additional controls, formatting and searchy goodness that can be tapped into but if I covered all that I would be writing this blog well into my retirement.

Search Result Sources

Before we start on the configuration of the display templates we need to determine what is going to be displayed.  As this search page is only going to be servicing my knowledge base site I am going to create a new Result Source that I can target from search web parts (this will make more sense late, I promise).

– To the “Site settings” I go and I click on “Result Sources” under “Search
– Click on “New Result Source
– Give it a name and set the other options as shown below

Edit result set source

– Now click on “Launch Query Builder

The goal here is to make sure that when we enter a search term into the search web part on the page it only searches the current site and only on Wiki pages.

– From the query builder ensure that “Query after all transformations” is showing in the “Keyword filter” box and then click “Add keyword filter” this will add the {searchterms} to the query text.  This basically is telling SharePoint to use the term that is provided to search on…again this will make more sense later.

CSWP adding a keyword filter

– Next step is to add the site I want to target.  For this I simply type in the URL with the “Path” parameter

CSWP update path in query

– The last thing I want do is only target the Wiki pages so I need to add a content type via the property filter like so.

CSWP update content type

– Clicking “Test Query” should show all the pages within the Wiki…
– Click “OK” and we’re done here.

Configuring the search page

Let’s start by creating a new Wiki page (I’ve called this page BlogExample) and adding the search web parts that we require, these are the “Search Box” and the “Search Results” WPs which can be found in the “Search” category.

Add CSWP to page

So we’ve got our web parts installed on the page like so..

CSWP example page

Not very exciting and if you enter a search term it will bring back results for the entire SharePoint site/farm (depending on your permissions etc).

Edit the page and open the web part properties on the “Search Box”.  We need to tell this web part to send its results to the “Search Results” web part on this page.  Otherwise it will send the results to another page or even to our site search centre.

Click on the “Send queries to other web parts on this page and check the “Search Results – Default” check box.

CSWP customise search box

Click “OK” to save.  As a side note you can also change the design of this search box with display templates.

Now let’s edit the “Search Results” web part and go to its properties.

Remember that “Search Result Source” we created earlier? Lets connect it to this web part.  From the properties click on “Change Query

CSWP change query source

In the “Select a Query” drop down locate the “Search Result Source” that was created

CSWP select query

If there is anything in the “Query text” box just simply delete it.  Click on “Test query” and you should see all the wiki pages in the search result preview.

Click “OK” to exit the query builder.

Click “OK” to exit the web part properties. Now save the page.

You will notice that the page has search results showing, it you want to remove this function then edit the “Search Result Source” and change the {searchTerms} to {searchTerms?}

CSWP search result source

Search will still function properly (see below) so its a case of if you want a blank page to search from or one that has some content.

CSWP search result on page

Cool, so we have a search page that is only targeting the current site.  Next it’s onto the display templates.

Search Result Display Template

Navigate to “Site Settings” -> “Master Pages and page layouts” (you need to be a member of the “Design” role to access this setting)

Note, I am using Internet Explorer for this example because it allows me to use the “Open with Explorer” function which makes editing these files a whole lot easier.

Once you’re in the “Master pages and page layout” library go to “Display Templates” -> “Search” and here you will find all the display templates relating to search.

I am going to start with the “Item” template as this has the most customisation done to it.

Find the file called “Item Default.html” and copy it, I’ve called mine “Item_Default_Demo.html”  (don’t worry about the .js files are these are automatically created by SharePoint)

Search template location

Edit this file with your favourite editor and change the TITLE to the new name “Default Item Demo

Name the display template

Save the file.

Ok, if you look at the file you’ll see this:

Modify search display template

This file calls another “system” display template that enables some clever trickery within SharePoint i.e highlighting the keywords within the search results.  For this example I am not interested in these features as I am looking to present pages within the search results so I am going to comment this out.

Save the file.

Display template disable keyword

So we’ve made our first edit to the item display template, lets apply this and see what we’ve broken.

Go back to the page where the web parts we added and edit it.

Open the web part properties panel on the “Search Results” web part.

Expand the “Display Templates” section, select “Use a single template to display items” then search for the display template you’ve just made within the drop down.

Change display template

Click “OK

You’ll probably notice that all the results on the page have disappeared….don’t panic! This is expected as we’ve basically removed all the clever stuff SharePoint isn’t really sure what to display.  So we better help it out!

Go back to the display template within your editor.

In the green text at the top of the file you will see a line called “ManagedPropertyMapping”.  These are managed properties from search and as such we can lever these to show what’s held within the Search metadata on the page, depending on the properties we select.

To start off lets go for an easy one, Title.

In the display template file go down to where we commented out the line earlier and add the following text below it

<div><h2> _#= ctx.CurrentItem.Title =#_ </h2></div>

search display template ManagedPropertyMapping

Notice that the “Title” is added to the line after the second period.  This is basically saying pull the current item and show it’s title. The H2 formatting is just used to make the title stand out. Lets save that file and go back to SharePoint.

Refresh the page and you should now see a bunch of titles shown but nothing else, like so….

search result display template on page

Already we on the path to producing a template that suits our needs.

Now I want to lever some more of the managed properties with Search to display content on the search results page.  In this example I am using system manage properties but there is nothing stopping you creating your own if the default ones don’t provide the right results for you.  To edit/view managed properties they can be found in “Site Settings” -> Search -> “Schema”

I know which properties I want to use, these are:

PublishingPageContentOWSHTML = This the entire page with all the HTML formatting applied
WikiCategory = This is the wiki category that is associated with the Wiki pages

Before I can use these managed properties in my display template I need to add them to the “ManagedPropertyMapping” line in the display template file.

The format for adding a managed property to this line is:

‘<Current item property name>’:<Managed property name>’

For simplicity I am keeping both these names the same. So I add the following two items to the “ManagedPropertyMapping” line within the display template file.

‘PublishingPageContentOWSHTML’:’ PublishingPageContentOWSHTML’
‘WikiCategory’:’ WikiCategory’

Like so..

Display template - add content types

Now they are added here I can use them in my code.  Let’s add the following line below the one we added earlier

<div><b>Wiki Category:</b> _#= ctx.CurrentItem.WikiCategory =#_ </div>

Display template - add content types 2

Notice the managed property “WikiCategory” has been used.  For information the <DIV> tags are present to help the formatting of the template.

Save the file and go back to SharePoint.

Refresh the page and with all things being equal the Wiki categories will now be showing

Search display template on page with modifications

Right, we have the page title and the category that it is a member of.  Lastly we need to bring in the page itself.  So lets go back to our file editor and add the following line:

<div> _#= ctx.CurrentItem.PublishingPageContentOWSHTML =#_ </div>

Save the file and pop back to SharePoint one more time and do a refresh of the page.

Yeah! We have the pages showing in the search results

Search display template search results

Adding flare to the design

We have our basic search result display template all done and it’s showing the information in the desired format.  However I am sure we can tweak it to make it more presentable.  To do this we are going to use a custom style sheet to format the look of the results.

Lets create a CSS file, I like to place my CSS files in the Style Library.

Open the “Style Library” library and then open in “Explorer view” (IE again) and create a file.  For the demo my file is called “DisplayTemplate_Demo.css

Within this file copy the following CSS into it:

.box-style1 {
width: 100%;
height: 100%;
background-color: #ffffff;
border-radius: 2px 2px 2px 2px;
margin-bottom: 20px;
box-shadow: 0px 0px 5px 1px rgba(0,0,0,0.75);
padding: 10px 0px 10px 10px;

Save the CSS file.  Note this CSS will create boxes around each page result in the search result web part.  Make sure you check the file in with a major version number.

Go back to the “Display Template” file we were editing earlier and add the following below the <body> tag:

$includeCSS(this.url, “~sitecollection/Style Library/CustomCSS/DisplayTemplate.css”);

(change the path to suite the location where you’ve saved your CSS file)

Search display template modifying CSS

The last step is to edit this line:

Search display template CSS modification before


Search display template CSS modification after

This tells the display template to use the custom CSS that we’ve just created.

Save the file, return to SharePoint and refresh the page.

All the search results should now be nicely formatted within boxes.  If you want to tweak the width/padding etc. this can be done through the custom CSS file we created.

Search display template finished layout

One last thing to remember is once you’re happy with your work remember to check in and publish as a major version any files you’ve been editing otherwise your work colleagues won’t be able to see your amazing work!

Leave a Reply

Your email address will not be published. Required fields are marked *