Thursday, August 19, 2010

How to display the ID number on edit and display forms in SharePoint 2007?

  1. We will use the content editor webpart to add Javascript that will display the ID. However, most edit and display forms do not allow you to add a webpart. This can be easily rectified by adding "&ToolPaneView=2" to the URL in the browser address line.
  2. Add a content editor webpart right above the form. Set the "Chome Type" to none in the "Appearance" section of the webpart.
  3. Click on the "Source Editor" button of the webpart.
  4. Add the following HTML/Javascript:
<script type="text/javascript">

//
// Item ID in DispForm.aspx and EditForm.aspx
//
function DisplayItemID()
{
   var regex = new RegExp("[\\?&]"+"ID"+"=([^&#]*)");
   var qs = regex.exec(window.location.href);
   var TD1 = document.createElement("TD");
   TD1.className = "ms-formlabel";
   TD1.innerHTML = "<h3 class='ms-standardheader'>ID</h3>";
   var TD2 = document.createElement("TD");
   TD2.className = "ms-formbody";
   TD2.innerHTML = qs[1];
   var IdRow = document.createElement("TR");
   IdRow.appendChild(TD1);
   IdRow.appendChild(TD2);
   var ItemBody = GetSelectedElement(document.getElementById("idAttachmentsRow"),"TABLE").getElementsByTagName("TBODY")[0];
   ItemBody.insertBefore(IdRow,ItemBody.firstChild);
}
_spBodyOnLoadFunctionNames.push("DisplayItemID");
</script>