Display Template custom no results text

The Control Template is responsible for the information that is shown when there are no results returned. Located in the control template is a JavaScript If statement that checks if there are no results returned. If there are no results then a default variable (noResults) will be displayed. We are able to change this to what ever HTML and text we want. In this blog post I will explain who to change this. More information on display templates can be found on my blog post SharePoint 2013 custom display templates.

Solution

1. Edit the related Control Template.
2. Find the if statement that checks if there are no results returned.

<!--#_
if (ctx.ClientControl.get_shouldShowNoResultMessage())
{
_#-->
        <div class="_#= noResultsClassName =#_">_#= $noResults =#_</div>
<!--#_
}
_#-->

3. Remove the default variable noResults and if needed remove the default class noResultsClassName

<!--#_
if (ctx.ClientControl.get_shouldShowNoResultMessage())
{
_#-->
        <div class=""> </div>
<!--#_
}
_#-->

4. Change the default text and if need add a new class for styling.

<!--#_
if (ctx.ClientControl.get_shouldShowNoResultMessage())
{
_#-->
        <div class="MyClass">This is a custom no results text!</div>
<!--#_
}
_#-->

5.Publish the Control Template

Result

CustomNoResultsText

 

 

Display Template Show Limits Characters

Display templates are used to show the queried results in a attractive and useful layout. With the CQWP we used XSLT to format the data as required, but with display templates we need to use JavaScript. One of my most formatted field is the publishing page content field. In this blog post I will explain how you can limit the number of characters shown. In this post I also explain how to remove the HTML markup.

Solution publishing page content
1. Edit the related Item Template.
2. Add the PublishingPageContent to the ManagedPropertyMapping. For a clean example I removed all mappings besides Title and PublishingPageContent.

<mso:ManagedPropertyMapping msdt:dt="string">'Titel':'Title','PublishingPageContent':'PublishingPageContentOWSHTML'</mso:ManagedPropertyMapping>

3. Create a variable for the PublishingPageContent column.

var PublishingPageContentHTML = $getItemValue(ctx, "PublishingPageContent");

4. The PublishingPageContentHTML will contain the page content but with styling. The following code will remove the styling.

var div = document.createElement("div");
div.innerHTML= PublishingPageContentHTML;
var PublishingPageContentL = div.textContent|| div.innerText|| "";

5. In most situations we also want to show a maximum number of characters followed bu three dots. The following code will limit the amount of characters to 175 followed by the required dots. If the content is less then 175 characters long, no dots will be displayed.

var PublishingPageContent = "";
if (PublishingPageContentL.toString().length > 175) {
	PublishingPageContent = PublishingPageContentL.toString().substring(0,175) + "...";
}
else {
	PublishingPageContent = PublishingPageContentL;
}

6. Us the code below to display the PublishingPageContent.

<div>
   _#= $htmlEncode(PublishingPageContent ) =#_
</div>

 

Result

CSWP Result

Remove HTML markup with display templates

Display templates are used to show the queried results in a attractive and useful layout. With the CQWP we used XSLT to format the data as required, but with display templates we need to use JavaScript. One of my most formatted field is the publishing page content field. In this blog post I will explain how you can show the field without the formatting tags by using JavaScript.

Solution publishing page content
1. Edit the related Item Template.
2. Add the PublishingPageContent to the ManagedPropertyMapping. For a clean example I removed all mappings besides Title and PublishingPageContent.

<mso:ManagedPropertyMapping msdt:dt="string">'Titel':'Title','PublishingPageContent':'PublishingPageContentOWSHTML'</mso:ManagedPropertyMapping>

3. Create a variable for the PublishingPageContent column.

var PublishingPageContentHTML = $getItemValue(ctx, "PublishingPageContent");

4. The PublishingPageContentHTML will contain the page content but with styling. The following code will remove the styling.

var div = document.createElement("div");
div.innerHTML= PublishingPageContentHTML;
var PublishingPageContentL = div.textContent|| div.innerText|| "";

5. Us the code below to display the PublishingPageContent.

<div>
   _#= $htmlEncode(PublishingPageContent ) =#_
</div>

Result

CSWP Result

Custom date formats with display templates

Display templates are used to show the queried results in a attractive and useful layout. With the CQWP we used XSLT to format the data as required, but with display templates we need to use JavaScript. One of my most formatted field are the date fields. In this blog post I will explain how you can change the format of a date field by using JavaScript.

Solution date format
1. Edit the related Item Template.
2. Add the ArticleStartDate to the ManagedPropertyMapping. For a clean example I removed all mappings besides Title and ArticleStartDate in the code below.

<mso:ManagedPropertyMapping msdt:dt="string">'Titel':'Title','ArticleStartDate':'ArticleStartDateOWSDATE'</mso:ManagedPropertyMapping>

3.  Create a variable for the ArticleStartDate column.

var ArticleStartDate = ctx.CurrentItem.ArticleStartDateOWSDATE;

4. To be able to format the date we need to create a new variable of the type Date.

var localArticleDate = new Date(ArticleStartDate);

5. Us the code below to display the ArticleStartDate with the required format.

<div>
   _#= localArticleDate.format("dd-MM-yyyy")
</div>

 

Result

CSWP Result

SharePoint 2013 Promoted Results aka Best Bets

With SharePoint 2013 the term Best Bets is no longer used, the new term is Promoted Results. The concept of the feature is the same, an administrator can serve (promoted) results based on specific search phrases enter by a user. The promoted result will appear on the top of the search results, this is a powerful way to promoted search results within SharePoint 2013. Specific results can be brought forward to the users attention with minimal effect and without changing any difficult ranking mechanic or index.

Solution

1. Open SharePoint 2013 Central Administrator.
2. Open the Managed Service Applications and click on the Search Service Application.
3. Click on Query Rules.
QueryRules
4. Select the context (source), in this example I used the Local SharePoint Results (System).
QueryRulesSource
5. Click on New Query Rule.
NewQueryRule
6. Enter a Rule name.
RuleName
7. Specify the exact phrases for the promoted result, use a ; to separate multiple phrases.
8. In this example I will make a promoted link to a image library.
ResultPhrase
9. Click on Add Promoted Result.
AddPromotedResult
10. Enter the Title and the Description of the Promoted Result.
11. Specify the URL that will be linked to the promoted result.
PromotedResultConfig
12. Save all the changes.
13. The promoted result is created and ready for use.

Result

Custom Promoted Result

SharePoint 2013 Search Refinement panel

In SharePoint the refinement panel is used to narrow down the search results. SharePoint provides a set of default refiners containing result type, author and modified date. Compared to previous version changing the refiners is simplified and very straight forward. Existing refinement options can be added through the user interface and options can be set per refinement.

RefinementOptions

Change the refinement panel

1. Edit the Search results page.
2. Edit the the refinement panel.
RefinementPanelEdit
3. Click on Choose Refiners…
RefinementPanelChooseFilters
4. With the Refinement configuration available refiners can be added or removed and ordered.
RefinementConfiguration1

5. The following setting can be changed for every selected refiner.
RefinementConfiguration2
6. Apply the changes and publish the search results page.

Create new refinement options

1. Open Central administration and open the Manage service applications.
2. Select the Search Service Application and click on Manage in the ribbon.
3. Click on Search Schema under Queries and Results
SearchSchema
4. If you are adding a newly created column make sure the column is available in the Crawled Properties
5. If the column is not available preform and crawl.
5. Click on New Managed Property
New Managed Property
6. Enter the Property name and select the correct type.
Managed Property name
7. Select the following options; Searchable, Queryble and Retrievable.
Managed Property Settings
8. Select Yes – active by Refinable
RefinementYes
9. Click on OK to save the changes.
10. Preform a crawl to make the new refinement available.
11. Add the refinement option in the refinement panel.