Display SharePoint Item ID in List Forms

Not a lot out there about this so I hope you find this useful. If you have a requirement to display the ID of a SharePoint list item in a custom display or edit list form what is the best way to go about it? Well that probably depends upon circumstances surrounding the requirement, e.g. is it a one off? If so there are some clever solutions using javascript/jquery which you should look at:

http://blog.pentalogic.net/2010/07/showing-the-records-id-on-the-view-and-edit-forms/

http://blog.pathtosharepoint.com/2009/01/18/item-id-in-display-and-edit-forms/

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:

List item ID added to list form

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.

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

2 Responses to Display SharePoint Item ID in List Forms

  1. Pingback: Displaying a SharePoint Guid field (SPFieldGuid) – the FieldValue Class | 3 Guys On SharePoint Blog

  2. Pingback: Overriding SharePoint List Form Templates and Using Custom Form Templates | 3 Guys On SharePoint Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>