Modifying SharePoint 2010 blog comment form using RenderingTemplate

The concept of RenderingTemplate has been around since MOSS and in SharePoint 2010 (I’m using Beta 2) RenderingTemplate is used pretty much everywhere.

In my case I had a site based of a Blog Template which contained posts and comments. Each comment box had few controls available and I wanted to include my custom logic to incorporate capcha into the commenting process so that I have less spam on my blog. Essentially this would mean I have to modify how the existing comment box looks like and behaves and use my custom logic.

If you take a look at the following file (DefaultTemplates.ascx): located in
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES

you will find the definition like this:

<SharePoint:RenderingTemplate id=”BlogCommentsForm” runat=”server”>

which defines how commeting form will look like in SharePoint.Don’t make any changes there – all of the changes in DefaultTemplates.ascx  will be applied accross the board on each of your sites and site collection. To make your changes local, all you need to do is to create the ascx file of your own, say call it MyTemplates.ascx and copy the existing definition with some changes you need to make to accomodate  your required functionality.

Below if the rendering template that I used for my comments. One of the really cool new improvement if the following control

   <SharePoint:ListFieldIterator runat=”server” ExcludeFields=”PostTitle”/>

which not only iterates through all of the fields defined in the list and renders them; it also allows you to specify which fields to exclude. As an additional feature it will not render controls corresponding to fields you have already defined which is very handy.

<SharePoint:RenderingTemplate id=”BlogCommentsForm” runat=”server”>
 <Template>
  <table width=”275″ cellpadding=”0″ cellspacing=”0″ border=”0″>
    <tr><td><h3><asp:Label id=”BlogCommentsFormTitle” runat=”server” Visible=”true” Text=”<%$Resources:wss,comments_AddComment%>”/></h3></td></tr>
  <tr><td>
  <span id=’part1′ style=”padding-top: 10px”>
   <SharePoint:InformationBar runat=”server”/>
   <table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”100%”>
   <SharePoint:ChangeContentType runat=”server”/>
   <SharePoint:FolderFormFields runat=”server”/>
   <SharePoint:ListFieldIterator runat=”server” ExcludeFields=”PostTitle”/>
   <SharePoint:ApprovalStatus runat=”server”/>
   <SharePoint:FormComponent TemplateName=”AttachmentRows” runat=”server”/>
   </table>
   <table cellpadding=”0″ cellspacing=”0″ width=”100%” style=”padding-top: 10px”><tr><td width=”100%”>
   <wssuc:ToolBar CssClass=”ms-formtoolbar” id=”toolBarTbl” RightButtonSeparator=”&amp;#160;” runat=”server”>
     <Template_RightButtons>
      <SharePoint:SubmitCommentButton CssClass=”ms-ButtonHeightWidth2″ runat=”server” Text=”<%$Resources:wss,tb_submitcomment%>”/>
     </Template_RightButtons>
   </wssuc:ToolBar>
   <SharePoint:InitContentType runat=”server”/>
   <SharePoint:ItemHiddenVersion runat=”server”/>
   </td></tr></table>
  </span>
  </td></tr></table>
  <SharePoint:AttachmentUpload runat=”server”/>
 </Template>
</SharePoint:RenderingTemplate>

The ascx file you will create doesn’t have to have all of your templates defined; you can split up your definitions accross many files. Once you have defined your template you have to give it a unique name on the top of your template definition RenderingTemplate id=”BlogCommentsForm” .

Now that you have your form defined you need your site to start using it. In my case I am creating a new site definition based of the existing blog template located here:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\Blog

Inside this folder you will also find XML\Onet.xml  which will allow you to define various parameters and lists for your site template including the form that your blog comments will be using. The section responsible for defining comments form will start with the following:

 <AllUsersWebPart List=”302″ WebPartZoneID=”Left” WebPartOrder=”3″>

 Here, one of the properties: <lfwp:TemplateName> will define the name of the template as you named it in yourascx file.

That’s it, once your custom site definition is provisioned and site instantiated of it, you’ll be able to see your custom comments form show up.

Enjoy!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , . Bookmark the permalink.

Comments are closed.