Home > development, error, MOSS, MS SharePoint Sever, sharepoint, sps > Some javascript for Sharepoint Forms

Some javascript for Sharepoint Forms

Here is some javascript that works for IE, Firefox and Chrome (shd work for Safari as well, as it behaves much like Chrome does).

A simpler javascript could have been written, which I actually did first, but it fails for Chrome and Firefox.

Hide Fields on NewForms.aspx and any other forms you like.

Put this code on line 70 of NewForm.aspx….

can also make fields readonly using this code. Can hide people picker.

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("autoFillFields");
function autoFillFields()
{
      hideFldCtrl("Approval Status");
      hideFldCtrl("Email Status");
      hideFldCtrl("Request Created");
 
       hidePeoplePicker("nobr", "Sales Manager");
       makeReadOnly("Reference Customer");
}


function getTagFromIdentifierAndTitle(tagName, identifier, title)
{
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);
    for (var i=0; i < tags.length; i++)
   {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
     {
         return tags[i];
     }
  }
  return null;
}



function hideFldCtrl(title)
{
    var ctrl = getTagFromIdentifierAndTitle("input", "TextField", title);
    if (ctrl) ctrl.parentNode.parentNode.parentNode.style.display = "none";

    ctrl = getTagFromIdentifierAndTitle("select", "DropDownChoice", title);
    if (ctrl) ctrl.parentNode.parentNode.parentNode.style.display = "none";

    ctrl = getTagFromIdentifierAndTitle("TextArea", "TextField", title);
    if (ctrl)
   {
       if (title == "Post Meeting Summary Link")
         ctrl.parentNode.parentNode.parentNode.parentNode.style.display = "none";
       else
         ctrl.parentNode.parentNode.parentNode.style.display = "none";
  }

  ctrl = getTagFromIdentifierAndTitle("input", "DateTimeField_DateTimeFieldDate", title);
  if (ctrl)
  ctrl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style
.display = "none";
}



function findacontrol(FieldName)
{
     var arr = document.getElementsByTagName("input");
     for (var i = 0; i < arr.length; i++)
     {
         if (arr[i].title == FieldName)
         {
             return arr[i];
         }
     }
}


function makeReadOnly(title)
{
    var ctrl = findacontrol(title);
    if (ctrl) ctrl.readOnly = true;
    ctrl.style.color = '736F6E';
}

function hidePeoplePicker(tagName, title)
{
     var tags = document.getElementsByTagName(tagName);
     for (var i=0; i < tags.length; i++)
     {
          if (tags[i].innerHTML == title)
          tags[i].parentNode.parentNode.parentNode.style.display = "none";
     }

}

“Post Meeting Summary Link” is a Rich Text field, so needed one extra “parentNode” to be hidden.

Hope this helps–
Rehman.

Advertisements
  1. Andre Rentes
    April 29, 2011 at 9:46 pm

    Hello Rehman,

    First of all good post! However the function to make readonly works only with textbox and not with dropdownlist, radios, etc….
    I tried to use the disabled attribute but when I try to save my custom edit form it delete the values. Did you try do something like this!?

    thanks in advanced

  2. imran
    November 6, 2012 at 4:24 pm

    Hi
    What is the tagname and identifier of “hyperlink and picture” field type for “getTagFromIdentifierAndTitle” JAVA Script function

  3. Thom
    June 13, 2013 at 2:13 pm

    How to set date blank in custom editform?

  1. September 30, 2010 at 8:41 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: