Wednesday, January 22, 2014

Override Value Renderer in Display Templates - SharePoint 2013

We were using SharePoint 2013 Content by Search Web Part in one of our projects and we faced an issue in IE8 where content is getting truncated. In IE8, it trims the content (we noticed that to 90 characters) and adds ellipsis whereas the other browsers display the entire text.

After some investigation we found out that call to line3.overrideValueRenderer($contentLineText) in the display template is causing this truncation (We created this display template by making a copy of one of the OOB Display Templates). We could confirm this behavior by adding alerts just before and after the overrideValueRenderer call.

Before overrideValueRenderer($contentLineText) call - in IE8:


After overrideValueRenderer($contentLineText) call - in IE8:


As a solution to our problem we wrote a custom renderer by taking a copy of the contentLineText default renderer. We can find SharePoint 2013 default renderers in the Search.ClientControls.js file at the 15 hive:

\15\TEMPLATE\LAYOUTS\Search.ClientControls.js

In the default contentLineText renderer, there is a condition to check whether the current browser is Internet Explorer and if the version is less than or equal to 8 as highlighted below. If the condition met, it will truncate the text to 90 characters, which now I have increased to 240 in my custom renderer.

<!--#_
// Custom Data Renderer.
customContentLineText = function (a, b) {
    if (Srch.U.n(b)) b = 240;
    var c = a.defaultValueRenderer ? a.defaultValueRenderer(a) : Srch.ValueInfo.Renderers.defaultRenderedValueHtmlEncoded(a), d = !Srch.U.w(c) && c.length > b && !Srch.ValueInfo.isHtmlPropertyName(a.managedPropertyName) && (Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version <= 8);
    return d ? Srch.ValueInfo.Renderers.textTruncateEnd(a, b) : c
};

var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_picture3Lines_");

var linkURL = $getItemValue(ctx, "Link URL");
linkURL.overrideValueRenderer($urlHtmlEncode);

var line1 = $getItemValue(ctx, "Line 1");
var line2 = $getItemValue(ctx, "Line 2");
var line3 = $getItemValue(ctx, "Line 3");

line1.overrideValueRenderer($contentLineText);
line2.overrideValueRenderer($contentLineText);
line3.overrideValueRenderer(customContentLineText);

The custom renderer can be placed in the display template itself and can be called as highlighted in gray color.