Sunday, 13 November 2011

How to show ID field in EditForm.aspx and DispForm.aspx in Sharepoint

Hi,

We do not get ID field in DispForm.aspx and EditForm.aspx pages out of the box. However many times business needs this field to show up in this form because of their uniqueness. To accomplish it a javascript is most easy option and it can be applied via content editor webpart. To do this follow these steps

1. Go to the list
2. Click view item with any existing item or if you do not have any item then in the URL after listname add /DispForm.aspx .
3. In the next page add ?ToolPaneView=2 . This will open the page in editable form and the add a content editor webpart. Edit it to add the HTML content.
4. Paste the below script

<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("showID");

function showID()
{

var querystring = location.search.substring(1, location.search.length);
var ids = querystring.split("&")[0];
var id = ids.split("=")[1];
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 = id;
var Tr1 = document.createElement("tr");
Tr1.appendChild(Td1);
Tr1.appendChild(Td2);
var Location = GetSelectedElement(document.getElementById("idAttachmentsRow"),"TABLE").getElementsByTagName("TBODY")[0];
Location.insertBefore(Tr1,Location.firstChild);
}

</script>


5. save it and exit.

The same process you need to follow in case of EditForm.aspx as well. The ID field will be shown in read only mode in both the forms.

For Sharepoint 2010 content editor webparts can be edited in the following way  http://rahulrashu.blogspot.com/2011/10/how-to-add-html-content-into-content.html . You can use same script here as well but in sharepoint 2010 these forms open in form of modal windows so if you want to edit them you need to use direct URL here.

I hope this will help you out.

Thanks,
Rahul Rashu

No comments:

Post a Comment