How to get the value of a person or group field value in SharePoint 2013 using jquery.
I have used
$("input[title=Field Name]").text()
But it is retrieving null. any work around?
How to get the value of a person or group field value in SharePoint 2013 using jquery.
I have used
$("input[title=Field Name]").text()
But it is retrieving null. any work around?
No need for jQuery, you can do it with vanilla Javascript
All the info you want, need, get is on a hidden Input in a Form:
Since this Field is before the visible Input,
document.querySelector("input[id^='AssignedTo']")
Get the First Input which id starts with AssignedTo
Get's you that Input:
See the value
attribute above
eval(document.querySelector("input[id^='AssignedTo']").getAttribute('value'))
Gets the value and converts the String to an Array:
Then it is up to you what you want to do with a JavaScript Array:
The Email is inside the EntityData
Object:
Since this is the second item in the Array
the email is:
var assignedArr=eval(document.querySelector("input[id^='AssignedTo']").getAttribute('value'));
var dannyEmail = assignedArr[1].EntityData.Email;
$.each
, or .css()
, .attr()
or .change()
. Oh well, community is what drives success.
try the below code,
// Get People Picker Values
function getEditorPeoplePickerValues(fieldName) { // Field Title
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 + ";#";
}
}
calling:
getEditorPeoplePickerValues("Field Title");
Also if you have only 1 person column in the form then try this:
var sUser = $('div.ms-inputuserfield').text();
You can get the user details using the following syntax
function getUserLoginName(peoplePickerId){
var loginName = '';
var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerId + "_TopSpan"];
var users = peoplePicker.GetAllUserInfo();
if(users.length > 0) {
loginName = users[0].Key;
//email = users[0].EntityData.Email;
//title = users[0].EntityData.Title;
}
return loginName;
}
If you want to get user id you have to make a rest call. Below is one of the approach I am using
function getUserIdByLoginName(loginName) {
var userId = -1;
var requestUrl = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/siteusers(@v)?@v='" + loginName + "'";
$.ajax({
url: requestUrl,
method: "GET",
async: false,
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
userId = data.d.Id;
},
error: function (data) {
fail(data);
}
});
return userId;
}
The script you can write as follows
var loginName = getUserLoginName("peopleFieldId"); //e.g: getUserLoginName("supervisor");
var userId = getUserIdByLoginName(loginName);
This can be used to update your list item in SharePoint.
getUserIdByLoginName
function is going to return anything other than -1 because it's not using promises.