Thursday, December 23, 2010

Using javascript to hide People Picker SharePoint field based on DropDown value

< script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hidePeoplePicker");

function hidePeoplePicker() {
//alert("HIDE");
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";

//This code runs on the onchange event of the drop down list field
getField('select','Form Type').onchange = function() {getSecurityLevelValue()};

}

function showPeoplePicker() {
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="";

}




function getTagFromIdentifierAndTitle(tagName, identifier, title) {

//alert("tagName = " + tagName);

var len = identifier.length;
//alert("Len = " + len);

var tags = document.getElementsByTagName(tagName);
//alert("tags length = " + tags.length);

for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
var tt = tempString.indexOf(identifier);
var ss = tempString.length - len;
if(tags[i].title == "People Picker")
//alert("tempString = " + tt +"---"+ss );
if (tags[i].title == title && (identifier == "" ||
tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}




function getField(fieldType,fieldTitle) {

var docTags = document.getElementsByTagName(fieldType);

//alert("Length = " + docTags.length);

for (var i=0; i < docTags.length; i++) {

if (docTags[i].title == fieldTitle) {

//alert("Value in getField = " + docTags[i].value);

return docTags[i];

}

}

}

function getSecurityLevelValue() {

//alert("In getSecurityLevelValue");
selectedId = getField('select','Form Type').options[getField('select','Form Type').selectedIndex].value;

//alert("Value in hide =" + selectedId);

if(selectedId != 'Org change/ cost centre change')

{

//alert("NOT Specific Request Hide");

hidePeoplePicker();

}

else

{

//alert("Request Show");
showPeoplePicker();

}

}

</script>

Note:-

1) If you have more than one People Picker either in edit form or new form, in that case hidden field should be displayed first and then non hidden people picker in that form. It is done by changing Column order.

0 comments: