Sharepoint - Javascript - 从列表中的人员选择器列中获取值

时间:2014-05-15 09:59:51

标签: javascript sharepoint-2013 client-object-model peoplepicker

我的SharePoint列表中有一个人员选择器列。

我需要从此列中获取所有值(名称)。我正在使用Java脚本代码从SharePoint列表中获取数据。

我的代码如下:

此处“User”是列表中列的名称。

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;
                }
                }
            }
}
}

我知道我需要使用get_lookupValue。 但是如果人员选择器列中只有一个值,我会将其作为colListItem.get_item(User)[0].$2e_1; 我发现我需要使用开发人员工具$2e_1。 这是正确的方法吗? 还有其他更好的方法吗? 请建议一些文章,信息链接,因为我对sharepoint和客户端对象模型都很新。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

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));
            }
    });
}

答案 1 :(得分:-2)

这里解释了一个明确的解决方案 -

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

我获得价值的方式_User = colListItem.get_item(User)[0].$2e_1; 是错的。

可以使用get_lookupvalue检索用户。

希望这有助于所有新手分享并面临类似问题。

谢谢!

相关问题