Archive

Archive for July, 2013

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.

Search_Result_SP_2013

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.

Solution:

  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).
    Search_Result_SP_2013_Display_Template_1
  6. Replace this line with
    var currentQueryTerm = ctx.DataProvider.get_currentQueryState().k; if(ctx.ClientControl.get_shouldShowNoResultMessage() && currentQueryTerm!=””){
    (As shown in below image)
    Search_Result_SP_2013_Display_Template_2
  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).
    Search_Result_SP_2013_Display_Template_3
  11. Save the changes and that’s it, you will see the  expected result.