Brief Introduction into Display Templates

Display Template Series

In SharePoint 2013, for search result Web Part you have the ability to customise your search results. Yes in 2010 you could customise the content query Web Part and search results, but you had to use XSLT to do this. Something I never got round to learning.

In 2013 you have the feature of display templates. Display templates are the combination of html and generated JavaScript which enable the customisation of search results. Used in Search WebParts, Display templates control which managed properties are shown in the search results, and how they appear in the Web Part. You can find the display templates on the site by navigating to;

Site Settings > Master Page Gallery > Display Templates

The Search folder is where the files relevant for the Search Results Web Part are kept. The Content Web Parts folder is where the files relevant for the Content Search Web part are kept.  Now this is the gotcha that made me spend half my morning getting nowhere. If you only see just the .js files within any folder underneath Display Templates, it means you do not have the site collection feature “SharePoint server Publishing Infrastructure” activated. Once this feature is activated, you will see all HTML and corresponding .js files.

Create your own Display Template

I was going to write a step by step guide on how to create your own Display Template, but I believe a simple example by Erik Swenson is a good walkthrough video. All I can advise is;

  • Only ever modify the .HTML files. When you upload the html file back into the Display Template folder it will automatically produce the .js file for you.
  • Any JavaScript written within the HTML files needs to be surrounded by <!–#_ javascriptcode _#–> and any reference to JavaScript within HTML attributes needs to be surrounded by _#javascriptcode#_