Friday, July 23, 2010

SharePoint People Picker

Have you ever wondered if you can retrieve data like email, job title, etc, from the people picker control in SharePoint using Javascript? In short, yes it can be done and it is pretty simple.

Below is some javascript which accepts a parameter for the the position of the control, and returns the desired value.

function getEmailFromPoeplePicker(identifier)
{
var tags = document.getElementsByTagName('DIV');
for (var i = 0; i < tags.length; i++)
{
var tempString = tags[i].id;
if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0))
{
var pplHTML = tags[i].innerHTML;
var valReturn = pplHTML.indexOf(">Email");
if (valReturn > 0)
{
pplHTML = pplHTML.substr(valReturn + 12);
var ValuePos1 = pplHTML.indexOf(">");
var ValuePos2 = pplHTML.indexOf("");
if (ValuePos1 > 0 && ValuePos2 > ValuePos1)
return (pplHTML.substring(ValuePos1 + 1, ValuePos2));
}
}
}
return null;
}

if you wish to return any other details from the people picker simply modify this line :

pplHTML = pplHTML.substr(valReturn + 12);

Change the substr value of 12 to the following :

29 For the mobile number
12 Gets the Job Title

This is only 3 of many examples of how it can be done, feel free to comment if you have found a better way.

No comments:

Post a Comment