Customizing ListItem Forms Using RenderingTemplates

I guess, you all know the Tags and Notes functionality of SharePoint 2010 by now.
Page Content

 

I guess, you all know the Tags and Notes functionality of SharePoint 2010 by now. On the My Profile page you can see all your social comments (the tags and notes) you have given on the pages as well as the possibility to leave a note. This Note Board is actually a webpart (SocialCommentWebPart).

2011-09-13-CustomizingListItem-01.png

I was asked to add this webpart on the display form for a particular list, so users can comment on a list item. If you are not a developer you would probably start SharePoint Designer right now, create a custom Display Form and add the webpart to this form and voilá. But if you are a developer you want this solution to be deployable and maybe generic. So no SharePoint Designer (which, by the way, replaces the original code with it’s own DataViewWebPart and XSLT.)

RenderingTemplates

List forms are using RenderingTemplates. These templates define how controls are rendered. For instance, choice fields can be rendered as a dropdown list or radiobuttons. These templates can be found in the CONTROLTEMPLATES folder in the SharePoint root folder. Opening this folder will show you many ASCX files.

2011-09-13-CustomizingListItem-02.png

The one we need is the DefaultTemplates.ascx. This file contains many templates, such as CompositeField, BlogForm, CreatedModifiedInfo and ListForm. The last one mentioned is the template we need to customize and add the SocialCommentsWebPart.

In my Visual Studio 2010 SharePoint project I have added a SharePoint Mapped Folder item pointing to the CONTROLTEMPLATES folder. Then I created a new UserControl and deleted all the CodeBehind files. Then I modified the ASCX so no reference is present to the deleted CodeBehind:

1 <%@ Control Language="C#"   AutoEventWireup="false" %>

From the original DefaultTemplates.ascx I copied the ListForm template and renamed it to CustomListForm:

01 <SharePoint:RenderingTemplate id="CustomListForm" runat="server"
02     <Template> 
03         <span id='part1'
04             <SharePoint:InformationBar runat="server"/> 
05             <div id="listFormToolBarTop"
06             <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server"
07                     <Template_RightButtons> 
08                         <SharePoint:NextPageButton runat="server"/> 
09                         <SharePoint:SaveButton runat="server"/> 
10                         <SharePoint:GoBackButton runat="server"/> 
11                     </Template_RightButtons> 
12             </wssuc:ToolBar> 
13             </div> 
14             <SharePoint:FormToolBar runat="server" /> 
15             <SharePoint:ItemValidationFailedMessage runat="server" /> 
16             <table class="ms-formtable" style="margin-top: 8px;" border="0" cellpadding="0" cellspacing="0" width="100%"
17             <SharePoint:ChangeContentType runat="server"/> 
18             <SharePoint:FolderFormFields runat="server"/> 
19             <SharePoint:ListFieldIterator TemplateName="ListFieldIterator" runat="server"/> 
20             <SharePoint:ApprovalStatus runat="server"/> 
21             <SharePoint:FormComponent TemplateName="AttachmentRows" runat="server"/> 
22             </table> 
23             <table cellpadding="0" cellspacing="0" width="100%"><tr><td class="ms-formline"><img src="/_layouts/images/blank.gif" width='1' height='1' alt="" /></td></tr></table> 
24             <table cellpadding="0" cellspacing="0" width="100%" style="padding-top: 7px"><tr><td width="100%"
25             <SharePoint:ItemHiddenVersion runat="server"/> 
26             <SharePoint:ParentInformationField runat="server"/> 
27             <SharePoint:InitContentType runat="server"/> 
28             <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator=" " runat="server"
29                     <Template_Buttons> 
30                         <SharePoint:CreatedModifiedInfo runat="server"/> 
31                     </Template_Buttons> 
32                     <Template_RightButtons> 
33                         <SharePoint:SaveButton runat="server"/> 
34                         <SharePoint:GoBackButton runat="server"/> 
35                     </Template_RightButtons> 
36             </wssuc:ToolBar> 
37             </td></tr> 
38             <tr><td width="100%" style="height:300px"><PortalWebControls:SocialCommentWebPart runat="server" /></td></tr> 
39         </table> 
40         </span> 
41         <SharePoint:AttachmentUpload runat="server"/> 
42     </Template> 
43 </SharePoint:RenderingTemplate>

Just before the closing table tag, I added an extra table row and cell to add my SocialCommentWebPart. Since this webpart is in another assembly you need to register it. I used PortalWebControls as the tagPrefix:

1 <%@ Register TagPrefix="PortalWebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.Portal.WebControls"%>

(Note: in order to use this SocialCommentWebPart successfully, you’ll have to set up User Profiles for your webapplication)

It is important to deploy your ASCX file in the CONTROLTEMPLATES folder! Do not create a subfolder, it won’t work.

Configure the list definition

Next step is to configure the list to use this custom template. In my solution I have a list definition. In the schema.xml you have the <Forms> tag. Here are the forms defined for displaying, adding and editing items. There is a Template attribute to tell SharePoint which RenderingTemplate to use with that form.

01 <?xml version="1.0" encoding="utf-8"?> 
02 <List xmlns:ows="Microsoft SharePoint" Title="Berichten" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/Berichten" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/"
03   <MetaData
04     <ContentTypes
05     </ContentTypes
06     <Fields
07     </Fields
08     <Views
09     </Views
10     <Forms
11       <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" Template="CustomListForm" /> 
12       <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" /> 
13       <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" /> 
14     </Forms
15   </MetaData
16 </List>

In my example I have the template CustomListForm set for the DisplayForm. By default, the template ListForm will be used for all list items.

Basically, this is it. However, there is one thing… You probably use content types. I do. That’s why it didn’t work out for me in the first place. So, if you do use content types, and you have a list definition based on a content type, you also have to set your custom Rendering Template for your content type:

In the <FormTemplates> element, you have the three forms: <Display>, <Edit> and <New>. Set your custom rendering template and you’re good to go. Remember, this content type XML definition exists in both the element.xml of your Content Type as well as in the Schema.xml of your list definition!

So, how does it look like now? Here’s a screenshot of my Social Comments Webpart in the display form of a list item:

2011-09-13-CustomizingListItem-03.png

Override OOB templates?

SharePoint offers a lot of rendering templates OOB. Is it possible to override, for example, the default template ListForm? The answer is… Yes! You can. But there’s a catch.

Now, every 2 weeks some of my colleagues and I get together to share our knowledge and experience. I was discussing this topic, about Rendering Templates, when my colleague Waldek Mastykarz explained that he also used this technique in one of our products. That night, it became clear that you can override OOB templates simply by using the same name in your ControlTemplate file. So, if you use ListForm for your custom template, then every list would use your customized template. How cool is that? But what is the catch? That night we asked ourselves: what if another 3rd party solution also uses a custom ListForm template? Then you have 3 templates with the same name ListForm. What happens then? So, Waldek said his famous words: “One way to find out…” And he did. It turns out that SharePoint alphabetically sorts all the ControlTemplate files and the first one on that sorted list is the big winner! Hmmm… Conclusion: it is not 100% guaranteed that your overridden custom ListForm will be used. I guess you will need to be smart on how to name your ASCX file.

Advertisements

My top 10 tools for a better SharePoint developer experience

The purpose of this blog post is to present some of the tools that I use in my day to day SharePoint activities. Tools that really help hardcore developers to be more efficient in both development and troubleshooting.
Page Content

The purpose of this blog post is to present some of the tools that I use in my day to day SharePoint activities. I will not talk about the very rich Microsoft tools such as SharePoint Designer, InfoPath etc…I’d rather focus on tools that really help hardcore developers to be more efficient in both development and troubleshooting.

This list of tools can probably be largely extended, so don’t hesitate to add comments to present your favorite tools.

CKS Dev

Available at http://cksdev.codeplex.com/

CKS Dev adds very useful features to Visual Studio for SharePoint projects. It accelerates custom solutions deployment and helps in developing some components that are not proposed out of the box by Visual Studio 2010.

As you can see, a lot of valuable project items are made available:

2011-12-04-Top10Tools-01a.png

On top of that, some extra contextual menu items are available and are very useful:

2011-12-04-Top10Tools-02.png

Most of these options allow you to quickly deploy components without being forced to deploy the full WSP. It is particularly useful when deploying artifacts such as .aspx pages, master pages etc…and very handy when debugging code-behind from SandBoxed , Farm solutions and even Timer Jobs.

It’s definitely an added value for every SharePoint developer.

Developer Dashboard

Documentation : http://msdn.microsoft.com/en-us/library/ff512745.aspx

This one is built-in in SharePoint 2010 and makes it easy to take some performance metrics, helps identifying heavy components and understanding what’s going on behind the scenes since it also shows how SharePoint queries its SQL databases.

Once enabled on the page, you get some results about the overall page payload, the database queries and many other metrics:

2011-12-04-Top10Tools-03.png

Combining the use of the Dashboard with the built-in SPMonitoredScope class helps in getting a precise idea on how your components perform. As a practice, for important actions, you can imagine that kind of patterns in your code:

1 #if DEBUG
2             using (SPMonitoredScope scope = new SPMonitoredScope("..."))
3             {
4 #endif
5              
6                 //do some stuff
7 #if DEBUG
8             }
9 #endif

Using those preprocessing directives will ensure that some code blocks are always monitored specifically when the code runs in debug mode.

SPDisposeCheck

Available at http://archive.msdn.microsoft.com/SPDisposeCheck

This one is an old tool but still a must-have. It has an add-on for Visual Studio so that it can be executed against your code when the project builds.

It helps to identify memory leaks or worse, disposed objects that shouldn’t have been disposed, sometimes causing issues that are quite difficult to troubleshoot.

WSPBuilder

Available at http://keutmann.blogspot.com/2009/12/wspbuilder-2010-beta.html

This tool has also been around for a while. This was incredibly useful in SharePoint 2007 since Visual Studio 2008 did not provide enough out of the box capabilities with regards to SharePoint solutions deployment. WSPBuilder clearly filled some gaps.

Admittedly, I’m using it less than before since Visual Studio 2010 is way better than Visual Studio 2008 regarding SharePoint solutions deployment but WSPBuilder can still be useful for batch and automated builds.

ULS Log Viewer

Available at http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=ULSViewer&DownloadId=7482

ULS Log Viewer is an advanced tool that helps in diagnosing SharePoint issues. It has a “real time” log viewing feature :

2011-12-04-Top10Tools-04.png

that you can combine with advanced filtering options :

2011-12-04-Top10Tools-05.png

That makes it a very handy pal when working with SharePoint.

SPLIT (SharePoint Log Investigation Tool)

Available at http://split.codeplex.com/

This tool is way less advanced than ULS Log Viewer but is also quite handy since it leverages the usefulness of the correlation ID. Since SharePoint 2010, any error generates a correlation ID. With SPLIT, you just need to paste that correlation ID in the UI and you’ll get all the correlated log lines :

2011-12-04-Top10Tools-06.png

Fiddler

Available at http://www.fiddler2.com/fiddler2/

I use this tool very often. While we cannot consider it a SharePoint tool, it’s great help for the following :

  • Analyzing web traffic

2011-12-04-Top10Tools-07.png

  • Working on client-side artifacts payload

2011-12-04-Top10Tools-08.png

In the context of SharePoint, Fiddler is particularly interesting when working with :

  • The Client Object Model  since you can easily see what was returned by the web services, how the queries are built etc…

2011-12-04-Top10Tools-09.png

  • How tools integrate with SharePoint. For instance, to see how SharePoint Designer is able to interact with SharePoint

2011-12-04-Top10Tools-10.png

In the above screenshot, just opening the site with SharePoint Designer has generated the shown queries.

I remember that a while ago, I was charged with studying SharePoint Workspace and I used Fiddler to see what could be the server-side impact in terms of workload. Fiddler is definitely a great tool.

.NET Reflector

Available at http://www.reflector.net/

I don’t know if an introduction is needed since this tool has been around for a while. This is clearly the best .NET reverse engineering tool. Unfortunately, it is not free anymore.

A good substitute is dotNetPeek (http://confluence.jetbrains.net/display/NETPEEK/dotPeek+Early+Access+Program). The tool is still in Beta and is less advanced than .NET Reflector but it’s free 2011-12-04-Top10Tools-13.png.

PowerGui

Available at http://powergui.org/downloads.jspa

Since PowerShell is a lot more integrated to SharePoint than in the past, admins and developers often write PowerShell scripts to perform automated deployment, automatic setup etc…

PowerGui can really help since it supports debugging and IntelliSense for PowerShell.

2011-12-04-Top10Tools-11.png

SharePoint Manager

Available at http://spm.codeplex.com/

This tool is very useful to inspect SharePoint. Whether you need to view or modify any kind of SharePoint object, SharePoint Manager is your friend.

It exposes a user-friendly UI :

2011-12-04-Top10Tools-12.png

and is one of the most downloaded SharePoint related CodePlex projects

Of course, let’s not forget Visual Studio 2010 itself2011-12-04-Top10Tools-13.png. Features such as code analysis, the test suite and basically all the ALM Microsoft suite with TFS.