Tuesday, June 14, 2011

Display two columns with same label in newform.aspx of SharePoint list !

SharePoint does not allow creating two columns with the same name. If you still try to create more than 1 column with the same name then you will be prompted with an error message.

“The column name that you are entered is already use or reserved. Choose another name.”

But in many cases we may need two columns with same label. In this post we can see a work around for creating two labels with same text. Below is what we are going to achieve.

  1. Create the second column with a special character(City#)
  2. Edit the Newform.aspx and Editform.aspx page and insert a content editor web part in the below of the list web part.

    Note: you can append the following query string ?pageview=shared&toolpaneview=2 to the URL (http://mysite/ Lists/job/NewForm.aspx?pageview=shared&toolpaneview=2) to open in edit mode.
  3. Copy and paste the below code in the content editor web part using source editor.
<script type="text/javascript">// <![CDATA[
var tables;
tables = document.getElementById('ctl00_m_g_a3ac2436_2405_4e04_8afd_fa6f84b65d1b_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_TextField').parentNode.parentNode.parentNode.parentNode.parentNode;
var i;
var str;
<tables.rows.length;i++)  {  str = tables.rows[i].cells[0].innerHTML;  if(str.indexOf('#') > 1)
tables.rows[i].cells[0].innerHTML =str.replace("#", "");
// ]]></script>

Note: In the above code ctl00_m_g_a3ac2436_2405_4e04_8afd_fa6f84b65d1b_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_TextField is the ID one of the column in that list, for identifying parent table. (You can get it by viewing the Source code of the page).

1 comment: