Tuesday, August 20, 2013

Content Search Webpart – Display Templates – Substring function


Display templates intorduced in Sharepoint 2013 tells the web part two things and it does it’s work by creating an HTML page and a .js file.

 1.What properties to show.
2.And how to display all data.

Display Template uses $getItemValue funtion to fetch the value of Metadata Property for an item. For eg

var line2 = $getItemValue(ctx, "Description");

fetches the value of Description property. However if you want to fetch only the first 50 characters (say), you cannot apply the javascript substring() function directly on the line2 variable in the display template.

The workaround to this problem is call the toString() function on the variable before calling the substring() function. Please find the code below

var line2SubString = line2.toString().substring(0,50);

Hope this helps…
Happy Coding..

3 comments:

  1. If i have to use the this variable to display in the display how to do that?

    ReplyDelete
    Replies
    1. You can use _#=line2SubString=#_ in you display template to output this variable

      Delete
  2. Thank you so much! Trying to work out why this failed - now can group my search results nicely.

    ReplyDelete