SharePoint Pals
 | Sign In
How to Apply Display Templates to the Custom Content Search WebParts programmatically in SharePoint 2013
by Sathish Nadarajan 18 May 2015
Author
SharePoint MVP
Visits   
Today  :   1     Total  :    7145

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation