Get a People Picker’s Current Value on a Form
There’s a nice article over at the SharePoint Designer Team Blog about how to manipulate a List Form Field (AKA Data Form Field) using Javascript. It gives some great methods to accomplish this, but if you’d like to get the current value of a People Picker on a form (for validation purposes, most likely), the info isn’t there to do so reliably. I had to do this today, so I wanted to post what I came up with. As usual when I post these things, I’ve left in the alerts that I used for debugging in case it helps you.
// Find a People Picker’s value using its identifier (ff1, ff2, etc.)to find it in the page
function getPickerInputElement(identifier) {
var tags = document.getElementsByTagName(‘DIV’);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
//alert('tags[' + i + '].id = ' + tempString);
if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf(‘UserField_upLevelDiv’) > 0)){
//alert(‘HIT for ‘ + identifier + ‘ id=’ + tags[i].id + ‘ value=’ + tags[i].value);
var innerSpans = tags[i].getElementsByTagName(“SPAN”);
for(var j=0; j < innerSpans.length; j++) {
//alert('innerSpans[' + j + '].id = ' + innerSpans[j].id);
if(innerSpans[j].id == 'content') {
//alert('HIT for ' + identifier + ' id=' + innerSpans[j].id + ' innerHTML=' + innerSpans[j].innerHTML);
return innerSpans[j].innerHTML;
}
}
}
}
return null;
}[/sourcecode]
If the person picker is empty then dont work
return null.
Well, if it’s empty, then that seems like the right thing to return. What would you expect to receive? In any case, this is a very old post and you should consider it a starting point you can use for your own needs.
M.
“empty” not is “null”
the correct return is a empty string.
u return null because u dont find the person picker field.
i have wrote some code to recover the person picker field in the 2 situatios, empty or not empty.
yes!! Finally I found what I was looking for!!! Thank you!!
This was exactly what I was looking for. Thanks for posting it!
Hi Marc,AM working on list field validation …here am able to validate the all the fields except people picker field…when trying to find value in people picker its value as and if we enter the name in people picker and try to find value its getting message like(
surya:
There’s way too much code in your comment to sift through. It looks like you’re doing a lot of debugging, so keep at it. Isolate what you are doing with the People Picker until you get it right and go from there.
M.
will this work for View Form also? Can you post code for the same. My req is if current user name and user field name are same than i should enable edit button in View Form.
Raghu:
No, this will not work on a DispForm because there is no People Picker. You can use a selector to get the text value.
M.
Thanks Marc ! It is working fine…It helped me a lot…