Great. Hacky. But great. But what if you want a solution which is available more widely attached to a content type for example? In that case, custom rendering templates are your friend. If you haven’t delved into the rendering method used in SharePoint then you should. It makes for an extremely quick and clever way to modify the rendering of SharePoint elements. The basics of using custom rendering templates is beyond the scope of this post but I will probably post more on that shortly. Back to the task…
In my case I needed to find a way to add the item ID to both the display and edit list forms just above the content type (a client’s requirement). As I was already using a custom rendering template, which is a modified copy of the standard listform rendering template, this was my first thought. However the ID field is a pretty special field of type ‘Counter’ as can be seen in FLDTYPES.XML, that has no specific FieldTypeClass and hence is of type SPField when returned. This rules out the use of a FormField control (no rendering template associated with the field) or a TextField control (cannot cast the field to type SPFieldText). The answer? Simple. Just use a FieldValue control which will render out the value of the field associated with the control. After examining and comparing the rendering template with the source of the generated form I could see that the InitContentType control emitted the following mark up
<table cellpadding="0" cellspacing="0"> <tr> <td nowrap="nowrap" class="ms-descriptiontext"> Content Type: <span id="ctl00_m_g_cdce444d_65bf_453f_927b_0d44f0bb91e7_ctl00_ctl09_ctl00_InitContentType">People</span> </td> </tr> </table>
So to ensure the same output I needed to add similar markup to my rendering template that output the item ID by using the FieldValue control. To achieve this I added the following to me custom rendering template, just above the InitContentType control declaration.
<table cellpadding="0" cellspacing="0"> <tr> <td nowrap="nowrap" class="ms-descriptiontext"> Record Card ID: <SharePoint:FieldValue ID="recordCardId" runat="server" FieldName="ID" ControlMode="Display" /> </td> </tr> </table> <SharePoint:InitContentType runat="server" />
This left me with the desired outcome of the ID being shown at the bottom of the list form, as shown here:
To take this to completeness and make it even more reusable create a user control which contains the markup above and reference that in the rendering template by registering the tag as follows.
<%@ Register TagPrefix="ThreeGuys" TagName="DisplayItemId" src="~/_controltemplates/DisplayItemId.ascx" %>
Then simply declare the control where desired – in my case above the InitContentType control.
<ThreeGuys:DisplayItemId runat="server" /> <SharePoint:InitContentType runat="server" />
The advantage of doing so is then that could be used in other rendering templates if desired.