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

Sathish Nadarajan
 
Solution Architect
May 18, 2015
 
Rate this article
 
Views
10897

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">~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Control.js</property>
         <property name="ItemTemplateId" type="string">~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">~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/MyWebPart.Control.js</property>
         <property name="ItemTemplateId" type="string">~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.

Category : Search, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment