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:
- Display Templates Support HTML & JavaScript instead of XSLT (which is often quite cumbersome to work with).
- Display Templates based configurations support 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).
- 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)
- As the Display Templates are scoped to Site or Site Collection, they can be used for all kind of results, can be used within Content Search Web Part, Refiners and so on.
Recommend you to follow this MSDN Article on Display Templates to get an in-depth understanding of the topic.
By now we got a fairly good Idea on “What are Display Templates?” and now it is time to see them in action.
Business Scenario
- Additional User Profile Property should be added to User Profile Page which will hold the User’s LinkedIn Profile URL.
- There should be a provision to adjust the display position of this Property in Core Search Result Web Part.
Prerequisites
User Profile Service should be up and running
User Profile Synchronization should be configured
Search Service Application Instance should be configured appropriately Search Center Site Collection should be configured appropriately
order to see Display Templates in Action, we need to perform following steps:
- Create User Profile Property
- Start Full Crawl of Content Source for the User Profiles
- Create, Map and Configure Managed Property
- Start Full Crawl of Content Source for the User Profiles
- Create Display Template
- Create Result Type with Custom Display Template
- Test Display Template
Create User Profile Property
Go to Central Admin >> Manage Service Applications
https://howtodowithsharepoint.files.wordpress.com/2014/06/113.png?w=450&h=246
Click User Profile Service Application
https://howtodowithsharepoint.files.wordpress.com/2014/06/214.png?w=450&h=128
Click Manage User Properties
https://howtodowithsharepoint.files.wordpress.com/2014/06/310.png?w=450&h=191
Click New Property
https://howtodowithsharepoint.files.wordpress.com/2014/06/43.png?w=450&h=80
Enter Name, Display Name, Type & Length for the User Property as needed.
https://howtodowithsharepoint.files.wordpress.com/2014/06/53.png?w=380&h=300
Make sure Default Privacy Setting must be set to Everyone
https://howtodowithsharepoint.files.wordpress.com/2014/06/63.png?w=450&h=375
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.
https://howtodowithsharepoint.files.wordpress.com/2014/06/73.png?w=300&h=450
Once done with it click OK to add the User Property
https://howtodowithsharepoint.files.wordpress.com/2014/06/83.png?w=300&h=141
Once added you can find the new property under Custom Properties Section
https://howtodowithsharepoint.files.wordpress.com/2014/06/93.png?w=450&h=71
Go to Manage User Profiles
https://howtodowithsharepoint.files.wordpress.com/2014/06/103.png?w=450&h=189
Find the required User Profiles and Edit their Profiles by selecting Edit My Profile from ECB Menu
https://howtodowithsharepoint.files.wordpress.com/2014/06/114.png?w=450&h=179
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
https://howtodowithsharepoint.files.wordpress.com/2014/06/123.png?w=450&h=153
https://howtodowithsharepoint.files.wordpress.com/2014/06/133.png?w=450&h=36
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
https://howtodowithsharepoint.files.wordpress.com/2014/06/143.png?w=450&h=93
Click Content Sources
https://howtodowithsharepoint.files.wordpress.com/2014/06/153.png?w=195&h=300
Click People Search and select Start Full Crawl from ECB
https://howtodowithsharepoint.files.wordpress.com/2014/06/163.png?w=450&h=264
Wait till the Crawl Status become Idle
Create, Map and Configure Managed Property
Go to Search Schema
https://howtodowithsharepoint.files.wordpress.com/2014/06/173.png?w=170&h=300
Click Crawled Properties Link
https://howtodowithsharepoint.files.wordpress.com/2014/06/183.png?w=450&h=72
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 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
https://howtodowithsharepoint.files.wordpress.com/2014/06/193.png?w=450&h=240
New we need to create a new Managed Property and Map it with the crawled property shown in earlier step
Click Managed Properties Link
Click New Managed Property Link
https://howtodowithsharepoint.files.wordpress.com/2014/06/203.png?w=450&h=150
Enter Name, Description, Data Type for the Managed Property
https://howtodowithsharepoint.files.wordpress.com/2014/06/215.png?w=450&h=275
Select Searchable, Queryable, Retrievable, Sortable, Refinable and Safe as appropriate
https://howtodowithsharepoint.files.wordpress.com/2014/06/222.png?w=450&h=315
https://howtodowithsharepoint.files.wordpress.com/2014/06/232.png?w=450&h=284
Click Add Mapping Button
https://howtodowithsharepoint.files.wordpress.com/2014/06/242.png?w=450&h=89
On the Crawled Property Selection Screen
- Enter Property Name
- Click Find Button
- Select appropriate Crawled Property
- Click OK
https://howtodowithsharepoint.files.wordpress.com/2014/06/252.png?w=398&h=450
Save the Managed Property Definition
https://howtodowithsharepoint.files.wordpress.com/2014/06/262.png?w=450&h=125
In order to verify newly Created Managed Property
- Enter Property Name
- Click Image Button
You should be able to see Managed Property with all the settings you specified earlier.
https://howtodowithsharepoint.files.wordpress.com/2014/06/272.png?w=450&h=162
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.
https://howtodowithsharepoint.files.wordpress.com/2014/06/282.png?w=450&h=264
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
https://howtodowithsharepoint.files.wordpress.com/2014/06/292.png?w=441&h=300
Click Upload Design Files
https://howtodowithsharepoint.files.wordpress.com/2014/06/301.png?w=450&h=149
Click Location URL as shown below
https://howtodowithsharepoint.files.wordpress.com/2014/06/311.png?w=450&h=125
Click Display Templates Folder
https://howtodowithsharepoint.files.wordpress.com/2014/06/321.png?w=450&h=270
Click Search Folder
https://howtodowithsharepoint.files.wordpress.com/2014/06/331.png?w=450&h=189
Since we need to modify the template for People Search Results, we need to modify the template Item_Person
https://howtodowithsharepoint.files.wordpress.com/2014/06/341.png?w=450&h=240
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.
https://howtodowithsharepoint.files.wordpress.com/2014/06/351.png?w=450&h=51
Open the Item_Person_LinkedIn Template file and make the following modifications
- Change the Title
- Added the Managed Property we created in earlier steps
https://howtodowithsharepoint.files.wordpress.com/2014/06/361.png?w=450&h=131
Now you need to decide where exactly you want to place this Managed Property within the Display Template.
In our case, we are going to place this below User Name
Find the HTML Tag NameValue
Below this tag add the following code as shown below
- Check if LinkedInProfileUrl Managed Property is not Empty
- Add HTML snippet to display the value stored in LinkedInProfileUrl Managed Property
- Close the IF block
https://howtodowithsharepoint.files.wordpress.com/2014/06/371.png?w=450&h=123
Save the file and copy it back to the same location
https://howtodowithsharepoint.files.wordpress.com/2014/06/381.png?w=450&h=407
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.
https://howtodowithsharepoint.files.wordpress.com/2014/06/391.png?w=450&h=431
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
https://howtodowithsharepoint.files.wordpress.com/2014/06/40.png?w=450&h=401
Click Result Types
https://howtodowithsharepoint.files.wordpress.com/2014/06/411.png?w=300&h=253
Click New Result Type
https://howtodowithsharepoint.files.wordpress.com/2014/06/421.png?w=450&h=93
- Provide Name to the Result Type
- Select Result Source
- Select Display Template (the one we have just created)
- Save Result Type
https://howtodowithsharepoint.files.wordpress.com/2014/06/431.png?w=450&h=240
https://howtodowithsharepoint.files.wordpress.com/2014/06/44.png?w=300&h=103
We can see the newly created Result Type on Manage Result Type Page as shown below
https://howtodowithsharepoint.files.wordpress.com/2014/06/45.png?w=450&h=111
With this we are all done with the 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.
https://howtodowithsharepoint.files.wordpress.com/2014/06/46.png?w=450&h=263
From the above walk-through, we can conclude the fact that Display Templates are a simple but really powerful mode of customizing Search Result Page as per custom needs.
Hope this help someone in need.