Pages

Subscribe:

Thursday, November 22, 2012

Script to Disable the Fields in NewForm EditForm in SharePoint


Before adding the following script to the NewForm or EditForm we have to give the Reference of jQuery.js and SPServices.js either in Master Page or in the form where you what to add this script.

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

  $(document).ready(function() {
 
      var isUserOnly = false; //User group member only     
      var isAdmin=false;
      var groupName = "Admin Group";
 
   $().SPServices({

      operation: "GetGroupCollectionFromUser",

      userLoginName: $().SPServices.SPGetCurrentUser(),

      async: false,

      completefunc: function(xData, Status)
     
        {
        if($(xData.responseXML).find("Group[Name= Admin Group ']").length == 1)
            {
                isAdmin = true;            
            }
            else
            {
                isUserOnly=true;               
            }
          }
       });
      
       if (isUserOnly)
       { 
            //Disable DropDown
            $("Select[Title='Project']").attr("disabled", "disabled");             
           
           
            var Title =document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');

Title.disabled=true;

           
var assignedTo = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl04_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_downlevelTextBox');

assignedTo.disabled=true;

var assignedTodiv = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl04_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv');  

assignedTodiv.disabled=true;

var description = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');

description.disabled= true;

var description1  = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_iframe');

var StartDate = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate');
StartDate.disabled=true;
       
//Disable Calendar Image                   
var imgStartDate = document.getElementById('ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDateDatePickerImage');
imgStartDate.disabled=true;
       
                   
//MultiLine Rich Textbox
description1.disabled = 'disabled';
           
                       
//Disable Dropdown fields
DisableField("Project");
               
//Disable Multiline box     $('#ctl00_m_g_e8d7e5d3_7fc5_4496_b8b6_00a6a4f46062_ctl00_ctl04_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_iframe').contents().find('body').attr('disabled','disabled');

//PeoplePicker
$('nobr:contains("Assigned To")').closest("td").next("td").attr("disabled", "disabled");
$("div[id$='_UserField_upLevelDiv']").attr("contentEditable",false);
$("span[id$='_UserField']").find("img").hide();        
           
       }   
 }); 


//Function to Disable DropDown
 function DisableField(title)
{
    for(var i = 0; i < document.all.length; i++)
    {
        var el = document.all[i];
        // find html element with specified title
        if(el.title == title)
        {
            el.disabled = true; // disable

            // if the next element has a reference to the current element
            // then disable if as well
            if(i < document.all.length - 1)
            {
                var el2 = document.all[i + 1];
                if(el2.outerHTML.indexOf(el.id) > 0)
                {
                    el2.disabled = true;
                }
            }
            break;
        }
    }
}

</script>

No comments:

Post a Comment