I have a list where Administrator adds users and it contains information of the users(Employee Name, First Name, Last Name, Email etc). Employee Name is a people picker control and I need to populate the rest of the fields in form when administrator selects a person in the people picker field.
I was able to populate these fields for current user but failed in doing so for the user selected in people picker field. I am using SP 2013 and I have no access to infopath and so Javascript is my only option.
Thanks for reading and any suggestions are welcome.
function GetPeople(){
var personField = "Employee"; // this must match the display name of the people picker field
var assigned; // value from people picker field
var userEmail; // stores the assigned's email
var workPhone; // stores assigned's work phone
var acctName; // stores the login
// Get people picker value
assigned = $().SPServices.SPFindPeoplePicker({
peoplePickerDisplayName: personField,
checkNames: true
});
// intiate check names - don't have to do another timeout here as we should already
// have proper values here, simply doing it again as a secondary check
assigned.checkNames.click();
// get the users email from the SPFindPeoplePicker dictionary
acctName = assigned.dictionaryEntries[0].AccountName;
// Get user information
$().SPServices({
operation: "GetUserProfileByName",
async: false,
AccountName: acctName,
completefunc: function (xData, Status) {
workPhone = getUPValue(xData.responseXML, "WorkPhone");
}
});
// fill out the text boxes with information
getField('input','Contact Email').value = userEmail;
getField('input','Contact Phone').value = workPhone;
}
// gets the value of the called profile field
function getUPValue(x, p) {
var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
return $(this).find("Name").text() == p;
}).find("Values").text();
return thisValue;
}