SharePoint - Customize Display Templates

In SharePoint 2013 we have displayed templates to handle the display of search results in web parts. I would recommend to read this msdn article to understand display templates first.

I will give a walk-through on how do we customize a display template for a particular requirement (in this case to display e-mail address of person).

Prerequisites

Search should be configured and working in the system.

Solution

  1. Create a web part page “MySearch.aspx”, and added “Search Box” & “Search Results” web parts.

  2. Configure both the web parts that search box query goes to search result.



  3. Go to Site Settings, then Master Page Gallery, Display Templates and Search.

  4. Download “Item_Person.html” file, the display template for People Item.



  5. Change html to display Email – add the highlighted code as in the following:



    Inserted text (after NameField div)
    1. < div id = ”EmailField” > < !–#_  
    2. var encodedEmail = (has_email == true) ? $htmlEncode(ctx.CurrentItem.WorkEmail) : “No Email Available”;  
    3. var displayEmail = Srch.U.getSingleHHXMLNodeValue(hhProps, “workemail”);  
    4. if ($isEmptyString(displayEmail))  
    5. {  
    6.     displayEmail = encodedEmail  
    7. }  
    8. _#– > < div id = ”EmailValue” > _# = displayEmail = #_ < /div>  
    9. </div >  
  6. Upload the updated file to display template library and publish the file.

  7. Go to the MySearch page, Edit Page and Edit ‘Search Results’ Web part.

  8. Under ‘Display Templates’, chose ‘Use a single template to display items’ and select ‘People Item’.



  9. Save and exit the page.

  10. Search name in search box, it should return email id with People data.