Monday, March 11, 2013

Hiding SharePoint list columns from NewForm.aspx using JavaScript

There are times I create a column in a SharePoint custom list and I don't want anyone with Contribute rights to alter the default value. In the NewForm.aspx and EditForm.aspx pages I add the following script to a CEWP on the page.

<script type="text/javascript">
  function HideField(title){
   var header_h3 = document.getElementsByTagName("h3");
   for (var i = 0; i < header_h3.length; i++)
   {
       var el = header_h3[i];
       var foundField;
       if (el.className=="ms-standardheader")
       {
           for (var j=0; j<el.childNodes.length; j++)
           {
              if (el.childNodes[j].innerHTML == title || el.childNodes[j].nodeValue == title)
             {
                 var elRow = el.parentNode.parentNode;
                 elRow.style.display = "none"; //and hide the row
                 foundField = true;
                 break;
         }
      }       
    }
     if (foundField)
  break;
 }
}
HideField("Workspace");
HideField("Remote Item ID");
</script>

No comments:

Post a Comment