Posts Tagged ‘Display Templates’

SharePoint 2013: Creating and calling custom JavaScript function in search display template

October 23, 2017 Leave a comment

Hi Friends,

Recently I came across a requirement where I need to create a custom JavaScript function which will change the date formatting on the provided string date parameter.

I tried creating normal JavaScript function in display template and tried calling it from other part of template but the function was undefined and not found inside the display template, I got little bit confused as I have created the JavaScript function in same display template but still not able to find it in same display template while calling. We can have couple of ways to resolve the issue.

Solution 1:

After some googling I found the cause of the issue and the solution as well, as follows.

The possible reason is scoping of the display template not allowing to consider external JavaScript function as a part of the scope to call inside display template.

The solution is to create new function on window scope and the call it in the desired place inside display template.

window.MyTestFunction = function(myParam)
// your own logic to work on the param 'myParam'

Calling the function:

<a style="cursor:pointer" onclick='window.MyTestFunction(3)'>Call My custom JS function</a>

Solution 2:

  1. Create a JavaScript file (MySampleJS.js) in the Display Templates/Search folder.
  2. Add the custom JS function in to the MySampleJS.js file.
  3. Add the following code into thetag below to include the JS file in display template
$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Search/ MySampleJS.js");

Happy learning.


SharePoint 2013: Friendly URL’s in Publishing HTML Field is broken and showing FIXUPREDIRECT.ASPX as hyperlink in display template

June 12, 2014 4 comments

Hi Friends,

I came across a weird scenario recently, thought to share here so that it will be helpful for some of our friends.


In one of the article page we were having a Publishing HTML Field, content author added relative friendly URL as a hyperlink inside the field.  I.e. help/ithelp/lync

This page was supposed to be crawled and this Publishing HTML Field’s search managed property was used to display the column data in a search display template in content search web part (CBS).

The result displayed in content search web part was perfect except some of the hyperlinks are pointing to wrong pages.

The URL’s were pointing to FIXUPREDIRECT.ASPX page with some GUID’s as query string like following.


Analysis of the issue:

After analysing the issue for couple of hours found that it is something related to managed metadata terms as the query string formed had termsetid and termId as query sting parameters.

Also we found that only the friendly URL’s ware broken. The magic happening while the page is getting saved and SharePoint tries to resolve the friendly URL with available terms and replacing the URL with FIXUPREDIRECT.ASPX URL and same gets crawled. It is nothing to do with search display template.


We tried to find out the solution to resolve this issue but no luck; we have tried workaround to replace the friendly relative URL with non-friendly URL and it worked for us.

help/ithelp/lync   TO   help/ithelp/lync.aspx


SharePoint 2013: Display template in content search web part not retaining selection

March 27, 2014 3 comments

Scenario: While adding content search web part in a page or page layout using all user web part. The selected display template while exporting web part is not retained the selection of custom display template and default display template gets selected if we create new page out of the page layout or page. I have verified couple of times in all user web part and it is correctly pointing to the display template like following-

~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Test_Item_Contacts.js


Solution:  Found out the problem with ~ symbol and solution is to replace it with ASCII equivalent & # 126 ; (Remove space)

~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Test_Item_Contacts.js


SharePoint 2013 Remove ‘Nothing here matches your search’ from search results web part

July 3, 2013 9 comments

Hi Friends,

SharePoint 2013 search have huge potential, with the integration of FAST and SharePoint search it becomes very powerful search solution. There are couple of new search web parts added like Content Search web part which is excellent search web part. New search result rendering mechanism called display templates is provided which are similar like JSON to HTML templates.

We will discuss a scenario about Search result web part and the solution related to the problem using display templates.

Scenario: If we add a search result web part on page and above that a search box web part to enter search term and according to that search term we want search result web part will display results. When we add both these web parts on page and before entering the search term also the results web part show  “Nothing here matches your search” (as shown in below image), which we don’t want to display unless we enter something in search box.


Obviously we may think that there will be some web part property available which will help us to resolve this issue, but web part do not have any property like this. Here come display templates in our help, we will see how we can use display templates to achieve this.

Search Results web part of SP 2013 uses ‘Control_SearchResults.html’ display template which is available under Master Page Library –> Display Templates –> Search.


  1. Rename the ‘Control_SearchResults.html’ file to some other name e.g. ‘Control_Test_SearchResults.html’
  2. Open this file in any text editor like notepad.
  3. Change the Title in this HTML file(I have used Test Default Result for demo), this title will be available for selection in web part properties.
  4. Now we want to get the search text entered in to search box web part, for that we will use ctx object as following ctx.DataProvider.get_currentQueryState().k
  5. Search     if(ctx.ClientControl.get_shouldShowNoResultMessage()){    line in HTML file (as shown in below image).
  6. Replace this line with
    var currentQueryTerm = ctx.DataProvider.get_currentQueryState().k; if(ctx.ClientControl.get_shouldShowNoResultMessage() && currentQueryTerm!=””){
    (As shown in below image)
  7. Save the modified HTML file.
  8. Go to Master Page Library –> Display Templates –> Search and upload new control template.
  9. You will see that respective ‘Control_Test_SearchResults.js’ will be created by SharePoint.
  10. Now go to your page where Search Result web part is added, edit Search results web part. Under Display Templates section select newly uploaded display template.( as shown in below image).
  11. Save the changes and that’s it, you will see the  expected result.