1

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?

1
  • 2
    You need to specify where you have this field, and e.g. a screenshot would become helpful. Depending on the case, using SPUtility.js or REST might become easier and more useful, but probably jQuery can be used for it too.
    – moe
    Jun 6, 2017 at 9:40

4 Answers 4

2

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;
4
  • +1 for javascript. jQuery fanatics are for the lazy!
    – Mike
    Jun 6, 2017 at 17:12
  • 1
    jQuery was great from 2006 till IE9 was released. $.ajax might be the only reason why you would use it today, but the Fetch API (and its polyfill for IE<12) is the future Jun 6, 2017 at 17:17
  • I cringe when I see $.each, or .css(), .attr() or .change(). Oh well, community is what drives success.
    – Mike
    Jun 6, 2017 at 17:25
  • I switched to WebComponents.... and am about to leave the Microsoft world/community that thinks React (its the new jQuery) has a future Jun 6, 2017 at 18:07
1

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();
1

try this $('.ms-entity-resolved')[0].textContent

0

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.

3
  • not sure that getUserIdByLoginName function is going to return anything other than -1 because it's not using promises.
    – Mike
    Jun 6, 2017 at 16:36
  • I am using 'async: false'. So it will return a value. This snippet has been tested. Jun 6, 2017 at 16:37
  • Yep, you are right -- completely missed the async false line. Sorry Venkat. Make sure you are using an older version of jQuery, since the async:false is deprecated. I wouldn't recommend this method, though. See here: sharepoint.stackexchange.com/questions/157625/…
    – Mike
    Jun 6, 2017 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.