How to Apply Display Templates to the Custom Content Search WebParts programmatically in SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   18 May 2015
Visit Count
Today :  2    Total :   8247
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


In one of the Older Article, we saw how to create a custom content search webpart. In this, let us learn about applying the display templates (Control Templates and Item Templates) to that WebPart programmatically.

Because, we will be creating the WebParts and the Display Templates separately. After the webpart and the display templates were deployed, we will be adding the webpart to the page and apply the display template to that webpart. But, this will require a post deployment steps. To avoid this, we can set the display template programmatically when the webpart is getting deployed. This will eliminate the manual step, but one sequencing constraint is, the Display Templates should be preset (deployed prior to WebParts WSP), otherwise exception will be thrown and the webpart will not be rendered properly. Moreover the exception is not relevant also. Hence, we need to give more attention to this sequence.

As soon as we create a webpart, the webpart will be created and modify the webpart file as below.

 <?xml version="1.0" encoding="utf-8"?>
 <webParts>
   <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
     <metaData>
       <type name="MyWebPart, $SharePoint.Project.AssemblyFullName$" />
       <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
     </metaData>
     <data>
       <properties>
         <property name="Title" type="string">MyWebpart</property>
         <property name="Description" type="string">My Content Search Web Part</property>
         <property name="RenderTemplateId" type="string">&#126;sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Control.js</property>
         <property name="ItemTemplateId" type="string">&#126;sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Item.js</property>
         <property name="ShouldHideControlWhenEmpty" type="bool">False</property>
       </properties>
     </data>
   </webPart>
 </webParts>
 

The Tags,

 <property name="RenderTemplateId" type="string">&#126;sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Control.js</property>
         <property name="ItemTemplateId" type="string">&#126;sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Item.js</property>
 

Are responsible for applying the display templates. And one important thing is usually we will be having a tilt symbol before the keyword called sitecollection. i.e., ~sitecollection/_catalogs/….

But, that will not work here. We need to put the encoded value of ~. i.e., &#126;

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan