Tuesday 11 March 2014

Custom Display Templates in SharePoint

Custom Display Templates in SharePoint
In content Search webpart, we can use custom display templates  (Control and Item Templates). To create custom templates, first download out of box one and then do some changes, then upload to same place. In content search webpart change Control and Item Templates.
Open SPO Site
Setings-->Design Manager
Edit Display Templates
For Content Search webpart custom templates..
Download
Control_List.html
Item_TwoLines.html
Opne above 2 files and change title, ID
Save and rename it
Open SharePoint designer
Open SPO site
Click on All files
_catalogs
Mansterpage
Display Templates
Search
Now Click on Imports Files
Select 2 files, one by one..
Now got to SPO
Add a Content Search webpart
Edit webpart
In Display Templates
You can find: Control and Item
Select dropdowns..
You will find new templates, which you have uploaded
Now you can change the Item and Control templates code..and upload ....like this you can change the display for Content Search Webpart
Ex: Open renamed item html file.
Search for <mso:ManagedPropertyMapping msdt:dt="string">
You will find: 'Link URL'{Link URL}:'Path',
After that add below code
'MyTitle'{MyTitle}:'MyTitleOWSTEXT',
In the above line, MyTitle is the column name in the list.MyTitleOWSTEXT is managed property.
refer how to find managed property.
Add below code after: linkURL.overrideValueRenderer($urlHtmlEncode);
var MyTitle = $getItemValue(ctx, "MyTitle");
MyTitle.overrideValueRenderer($urlHtmlEncode);
var MyTitleId = encodedId + "MyTitle";
Add below code inside div
<div class="ms-Wrap" title="_#= $htmlEncode(MyTitle.defaultValueRenderer(MyTitle)) =#_" id="_#= MyTitleId =#_">
_#= MyTitle =#_
</div>
Save and Upload item template.
How to find a managed property name in SharePoint 2013
Open SharePoint site
Settings
Site Settings
find: Search Schema (Under …Site Collection Administration)
Click on: Search Schema
Type your site column name which you want to find Managed property, at Managed property Text box.
Click –> button.
You will find Managed property of your site column.
Note: SharePoint will automatically create managed properties for site columns.
So when you are working with Crawl, create a site column and add that site column to List.