SharePoint 2013: How To Create Custom Display Templates For SharePoint Search

What are Display Templates

Display Template is a new technique introduced in SharePoint 2013 in order to modify the look and feel of the search results by making use of HTML & JavaScript instead of XSLT modifications as we did in the earlier versions.

With this new rendering technique or rather framework, SharePoint offers some of the most compelling advantages as mentioned below:

  1. Display Templates Support HTML & JavaScript instead of XSLT (which is often quite cumbersome to work with).

  2. Display Templates based configurations supports a broader scope of execution than XSLT based configurations. (As Display Templates should be defined at Site or Site Collection Scope while XSLT based Templates could only be defined at the WebPart Scope).

  3. Display Template applies Per Item Basis and not Per Result Set Basis (Provides us with the Context of the Current Item which is under execution, so that we can make use of any Managed Metadata Property associated with the Search Result Item and render it as per needs).

  4. As the Display Templates are scoped to Site or Site Collection, they can be used for all kind of results, can be used with in Content Search Web Part, Refiners and so on.

I recommend you follow this MSDN Article on Display Templates to get an in depth understanding on the topic.

By now we have  a fairly good Idea on “What are Display Templates?” and now it is time to see them in action.

Business Scenario:

  1. Additional User Profile Property should be added to User Profile Page which will hold the User’s LinkedIn Profile URL.
  2. There should be a provision to adjust the display position of this Property in Core Search Result Web Part.

Prerequisites:

  1. User Profile Service should be up and running.
  2. User Profile Synchronization should be configured.
  3. Search Service Application Instance should be configured appropriately.
  4. Search Center Site Collection should be configured appropriately.

In order to see Display Templates in Action, we need to perform following steps:

  1. Create User Profile Property.
  2. Start Full Crawl of Content Source for the User Profiles.
  3. Create, Map and Configure Managed Property.
  4. Start Full Crawl of Content Source for the User Profiles.
  5. Create Display Template.
  6. Create Result Type with Custom Display Template.
  7. Test Display Template.

Create User Profile Property,

Go To Central Admin >> Manage Service Applications

Service

Click on User Profile Service Application,

User Profile Service Application

Click on Manage User Properties,

Manage User Properties

Click on New Property,

 New Property

Enter Name, Display Name, Type & Length for the User Property as needed.

Property

Make sure Default Privacy Setting must be set to Everyone,

Default Privacy Setting

Make sure Indexed must be selected under Search Settings Section, else this property will be skipped by Search Crawler and it won’t be available to use later.

Indexed

Once done with it click OK to add the User Property,

8

Once added you can find the new property under Custom Properties Section,

Custom Properties Section

Go to Manage User Profiles,

Manage User Profiles

Find the required User Profiles and Edit their Profiles by selecting Edit My Profile from ECB Menu,

Profiles

On the User Profile Page, locate the LinkedIn Profile Property,

Enter the URL to LinkedIn Profile of the User and Save the User Profile Properties,

LinkedIn Profile

LinkedIn Profile

With this we are all done with the Creation of User Profile Property.

Start Full Crawl of Content Source for the User Profiles,

Now in order to generate the Crawled Property corresponding to the User Profile Property we need to run the Full Crawl on the People Content Source,

Go to Search Service Application,

Search Service Application

Click on Content Sources,

Content Sources

Click on People Search and select Start Full Crawl from ECB,

Start Full Craw

Wait till the Crawl Status become Idle,

Create, Map and Configure Managed Property,

Go to Search Schema,

Search Schema

Click on Crawled Properties Link,

Crawled Properties Link

We should verify if the Crawl Property corresponding to our User Profile Property has been created successfully or not,

Find the Crawled Property by entering Search Term in Crawled Properties Textbox and click on the Image button with Green Arrow image.

Make sure you should be able to see the Crawled Property which is created during the Full Crawl Process as shown below,

Properties

New we need to create a new Managed Property and Map it with the crawled property shown in earlier step,

Click on Managed Properties Link,

Click on New Managed Property Link,

Managed Properties Link

Enter Name, Description, Data Type for the Managed Property,

Managed Property

Select Searchable, Queryable, Retrievable, Sortable, Refinable and Safe as appropriate,

searchable

sortable

Click on Add Mapping Button,

Add Mapping Button

On the Crawled Property Selection Screen,

  1. Enter Property Name
  2. Click Find Button
  3. Select appropriate Crawled Property
  4. Click OK

Crawled Property Selection Screen

Save the Managed Property Definition,

Managed Property Definition

In order to verify newly Created Managed Property,

  1. Enter Property Name
  2. Click Image Button

You should be able to see Managed Property with all the settings you specified earlier.

Property

With this we are done with the creation of require Managed Property corresponding to the Crawled Property,

Start Full Crawl of Content Source for the User Profiles,

Now in order to fill the Managed property with the value stored with in Crawled Property based on the mapping, we need to run the Full Crawl again as we did before.

User Profiles

Create Display Template,

Once we are done with the Creation and Configuration of required Managed Property, it is time now to create a new Display Template which will include this Managed Property.

We need to launch Design Manager from the Standard Menu as shown below,

Design Manager

Click on Upload Design Files,

Upload Design Files

Click on Location URL as shown below,

Location URL

Click on Display Templates Folder,

Display Templates Folder

Click on Search Folder,

Search Folder

Since we need to modify the template for People Search Results, we need tomodify the template Item_Person,

Item_Person

Point of Caution:

As a part of recommended SharePoint Practices, we should never edit and existing template, instead we should make a copy of it, rename it and then modify it as per our needs.

modify

Open the Item_Person_LinkedIn Template file and make the following modifications,

  1. Change the Title
  2. Added the Managed Property we created in earlier steps

Property

Now you need to decide where exactly you want to place this Managed Property within the Display Template.

In our case I am going to place this below User Name,

Find the Html Tag NameValue.

Below this tag add the following code as shown below,

  1. Check if LinkedInProfileUrl Managed Property is not Empty.
  2. Add HTML snippet to display the value stored in LinkedInProfileUrl Managed Property.
  3. Close the IF block.

block

Save the file and copy it back to the same location.

location

SharePoint generates a JS File corresponding to this HTML file automatically, and the JS file should not be modified by the users. This JS file will be maintained and modified by SharePoint system automatically based on the changes you have made in HTML File.

html

With this we are done with the development of new Display Template.

Create Result Type with Custom Display Template,

Now the next step is to create a new Result Type, which will be using our new Display Template to display the Search Results,

Go to Site Settings,

Site Settings

Click on Result Types,

Result Types

Click on New Result Type,

New Result Type

  1. Provide Name to the Result Type
  2. Select Result Source
  3. Select Display Template (the one we have just created)
  4. Save Result Type

Result Source

44

We can see the newly created Result Type on Manage Result Type Page as shown below,

Result Type

With this we are all done with development of new Display Template, it is time to test it now.

Test Display Template,

Search for the Users and sure enough you will find the new Property appears only for those User Profiles which contains a non-empty value for LinkedIn URL Property.

Property

From the above walk through we can conclude that Display Templates are a simple but really powerful mode of customizing Search Result Page as per custom needs.

Hope this helps someone in need…

Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all