SharePoint (2003 thru Online): Trick to Edit Web Part pages ( NewForm, DispForm & EditForm )

Wednesday, February 4, 2015

Trick to Edit Web Part pages ( NewForm, DispForm & EditForm )


Sometimes we need to edit and add scripts to the NewForm.aspx, DispForm.aspx & EditForm.aspx based on user requirements.


Here we are trying to add the ID field in the NewForm.aspx.

First, append ?ToolPaneView=2 to the URL to switch to edit mode as shown below.

http://spdev.dev.com/Lists/Announcements/NewForm.aspx?ToolPaneView=2


Next, add a hidden Content Editor Web Part (CEWP) to the page

Then add the below script in the source editor of the CEWP, To Hide the fields.

<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("Title");

   control.parentNode.parentNode.style.display="none";
   control = findacontrol("Link");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("Expiry Date");
   control.parentNode.parentNode.style.display="none";
}
</script>

________________________________________________________

Next, add a hidden Content Editor Web Part (CEWP) to the page

Then add the below script in the source editor of the CEWP, To Display the ID in the first row.

<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'>Issue 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];

// To display the ID in the first row
ItemBody.insertBefore(IdRow,ItemBody.firstChild);

// To display the ID in the last row (enable the below line)
//ItemBody.appendChild(IdRow);

}
_spBodyOnLoadFunctionNames.push("DisplayItemID");
</script>

In the above script, you can change the "Issue ID" to any other text as you need.
You need to repeat the above steps to get it on to the EditForm.aspx.

No comments:

Post a Comment