Wednesday, September 4, 2013

Disable People Picker, Lookup Fields in SharePoint List Forms

In continuation with my earlier post: How to Make SharePoint List Column (Form Field) Read Only, There are questions on disabling (or making them read-only) People Picker, Lookup Fields in SharePoint List Forms. Here I'm sharing the jQuery scripts to disable people picker / Lookup fields in SharePoint list forms (NewForm.aspx, EditForm.aspx).

jQuery to Disable People Picker Fields in SharePoint List Forms:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
 //People Picker Field to Disable 
 var FieldName = "Project Manager";

 //Get People Picker using its field name
 var element=$("tr:contains("+FieldName+"):last").find("div[title='People Picker']");

 //Disable People Picker
 element.attr('disabled','true');  

 //Disable "Check" and "Browse" Buttons in People Picker field
 $(this).find("a[Title='Check Names']").click();
 $(this).find("a[Title='Browse']").attr("onclick","");
});

//Enable the fields during "PreSaveAction" otherwise, their values will not get saved!
function PreSaveAction()
{
var FieldName = "Project Manager";
var element=$("tr:contains("+FieldName+"):last").find("div[title='People Picker']");
element.removeAttr('disabled');

//Enable Save
return true;
}
</script>

How to to Disable Lookup Fields in NewForm/EditForm:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {

//Get the field using its  name
var element=$('nobr:contains("Parent Project")').closest("td").next("td");

//Disable Entire TD
element.attr('disabled','disabled');  
});

//Enable the fields during "PreSaveAction" otherwise, their values will not get saved!
function PreSaveAction()
{
var element=$('nobr:contains("Parent Project")').closest("td").next("td");
element.removeAttr('disabled');

//Enable Save
return true;
}

</script>
Same code works to disable Multi-Choice fields in SharePoint NewForm.aspx or EditForm.aspx. The above code can made shorten as:
$('nobr:contains(“Parent Project")').closest("td").next("td").attr("disabled", "disabled"); 
I wrote it eloborated.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. Thanks for the post. Your site is very helpful!

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...