SharePoint Online SPUtility GetValue empty for People Picker
-
11-12-2020 - |
Question
I am trying to retreive the value of a people picker using SPUtility v0.14.2 in SharePoint Online though it always returns an empty string.
My script is as follows, do I need to retrieve the value using a different method?
$(document).ready(function () {
var facilitator = SPUtility.GetSPField("Facilitator");
$(facilitator.Controls).focusout(function() {
var f = facilitator.GetValue();
console.log(f);
});
});
Solution
No you cannot directly pick values from People picker fields. Reason being a people picker field is nothing but a lookup to a list where SharePoint stores various users. Some of the values associated with a people picker field are:
--> Id
--> Display Text
Still it can be done in the following way (Workaround):
Just paste the following code inside PreSaveAction()
function PreSaveAction()
{
//alert('PreSaveAction');
// Get People Picker Values
function getEditorPeoplePickerValues(fieldName)
{ // Field Title
var editorNames = "";
var _PeoplePicker = $("div[title='" + fieldName + "']");
var _PeoplePickerTopId = _PeoplePicker.attr('id');
var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
editorsInfo = ppobject.GetAllUserInfo();
var i;
for (i = 0; i < editorsInfo.length; ++i) {
editorNames += editorsInfo[i].DisplayText ;
}
return editorNames;
}
var UserName = getEditorPeoplePickerValues("You fields name");
}
Just enter you people picker field's name in the double quotes.
Note: I assume you have already added reference for SPUtility and Jquery.
OTHER TIPS
I did it using only SPUtility.js
without workaround.
Use
SPUtility.GetSPFieldByInternalName('Field_x0020_Internal_x0020_Name').GetValue()[0].EntityData.Email
for getting the Email Id
SPUtility.GetSPFieldByInternalName('Field_x0020_Internal_x0020_Name').GetValue()[0].DisplayText
for getting the Display Text
I am assuming it is a single user people picker. if it is Multi user people picker, just iterate on GetValue()[]
array and fetch all data you need