by Andreas

Sharepoint 2007 (Moss 2007) – hide fields on NewForm.aspx and EditForm.aspx

Every now and then you have lists that contain columns / fields that you need behind the scenes but that you don’t want the users to see. One option is to define custom forms in SP Designer, but if the automatic out-of-the-box forms are good enough just add a bit of Javascript to the page (thanks to Scott Wheeler over at Sharepoint Sherpa for this one):

1. Use Sharepoint Designer, open your site and expand the directory of your list

image

2. Find the form where you wish to hide one or more fields (I obviously really feel strongly about changing EditForm.aspx and NewForm.aspx) and open it in code view.

3. Locate the placeholder for the main content:

<asp:Content ContentPlaceHolderId=”PlaceHolderMainrunat=”server>

and insert the following script immediately after:

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

_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {

   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
   {
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }
   }
}

function hideFields() {

   var control = findacontrol("SomeFieldName");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("SomeOtherFieldName");
   control.parentNode.parentNode.style.display="none";

}
</script>

Now you can swap “SomeFieldName” to the name of one of your fields and it will disappear. Add as many fields as you wish (each field requires both the findacontrol() invocation and setting the style).