Question

I have a People Picker column in my SharePoint List.

I need to get all the values (names) from this column. I am using Java script code to get the data from SharePoint lists.

My code is as below:

Here “User” is the name of the column in the list.

var enumerator = listItem.getEnumerator();
while (enumerator.moveNext()) {

var _User = "";
if (colListItem.get_item(User) !== 'undefined' && colListItem.get_item(User) !== null) {
  //Check if people picker contains more than one value
                if (colListItem.get_item(User).length > 0) {
//Check if people picker contains only one value
                if (colListItem.get_item(User).length == 1) {
                    _User = colListItem.get_item(User)[0].$2e_1;
                }
//Check if people picker contains more than one value
                if (colListItem.get_item(User).length > 1) {
                for (var i = 0; i < colListItem.get_item(User).length; i++)
  {
  //Append all User names with a semi colon separator

               _User = _User + colListItem.get_item(User)[i].get_lookupValue() + ";";
                }
                        _User.trim;
                }
                }
            }
}
}

I know I need to use the get_lookupValue for this. But If there is a single value in the people picker column, I am getting it as colListItem.get_item(User)[0].$2e_1; I figured out that I need to use $2e_1, using developer tools. Is this the right way? Is there any other better way? Please suggest some articles , informative links on this as I am very new to sharepoint and also client side object model. Thanks in advance.

Was it helpful?

Solution 2

A clear solution is explained here-

http://gocodin.blogspot.in/2017/04/how-to-get-lookup-valueusername-from.html

My way of getting value _User = colListItem.get_item(User)[0].$2e_1; is wrong.

Users can be retrieved using get_lookupvalue.

Hope this helps all those who are new to sharepoint and facing similar issue.

Thanks!

OTHER TIPS

function getListData(ListId) 
{
    $().SPServices(
    {
        operation: "GetListItems",
        async: false,
        listName: "NodueFormList",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Text'>" + ListId + "</Value></Eq></Where></Query>",
        completefunc: function (xData, Status) 
            {
                var SeparationCount = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
                if (SeparationCount > 0) 
                {
                    $(xData.responseXML).SPFilterNode("z:row").each(function () {
                        if ($(this).attr("ows_Admin") != undefined) { 
                            AdminId = $(this).attr("ows_Admin").split(';')[0];
                        }
                        if ($(this).attr("ows_HR") != undefined) { 
                           hrId = $(this).attr("ows_HR").split(';')[0];
                        }
                  });    
            }
        }
    });
}

function UpdateData(ListId)
{

    var adminaddtional = document.getElementById("txtareaAdition").value;
    siteUrl = "/sites/Empower/";
    var listName = "NodueFormList";
    var itemType = GetItemTypeFoListName(listName);
    var item = 
    {
        __metadata: { "type": itemType },
    };
    $.extend(item, { PendingwithId: hrId });
    //Multiple people picker update IsVisible is column name and Id add
    $.extend(item, { IsVisibleId:{'results':[AdminId,hrId]}}**);
    $.extend(item, { Flag: "0" });
    $.extend(item, { Additonalnotetoadmin: adminaddtional });
    $.extend(item, { Role: "HR" });
    $.extend(item, { IsActive: "Yes" });
    $.ajax(
    {
        url: siteUrl + "_api/Web/Lists/GetByTitle('" + listName + "')/GetItemById('" + ListId + "')",
        type: "POST",
        async: false,
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
        "accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*",
        "X-Http-Method": "MERGE"
        },`enter code here`
        success: function (data) 
        {
            alert("Updated Successfully");

        },
        error: function (error) 
            {
                console.log(JSON.stringify(error));
            }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top