如何使用javascript将人员选择器数据(DisplayText)添加到“人或组”类型的列中?

时间:2018-01-02 09:57:13

标签: javascript sharepoint

我正在收到用户的显示文字和电子邮件,但我不知道如何将其插入“人员或群组”列。

function getUserInfo(PeoplepickerId) {  
    var UsersID="";
    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict[PeoplepickerId + "_TopSpan"];  

        if (peoplePicker.HasInputError) return false; // if any error  
        else if (!peoplePicker.HasResolvedUsers()) return false; // if any invalid users  
        else if (peoplePicker.TotalUserCount > 0) {  
            // Get information about all users.  
            var users = peoplePicker.GetAllUserInfo();  
            //var userInfo = '';  
            //var promise = '';  
             for (var i = 0; i < users.length; i++) {  
                 UsersID += users[i].DisplayText + "\n";
                 UsersID += users[i].EntityData.Email;
             }  
             return UsersID;  
        }       
    } 

1 个答案:

答案 0 :(得分:1)

您可以使用JSOM将数据插入SharePoint列表,如下所示:

var ctx = new SP.ClientContext.get_current();

var list = web.get_lists().getByTitle("Custom List");
var listCreationInformation = new SP.ListItemCreationInformation();
var listItem = list.addItem(listCreationInformation);           

// Get user keys.
var keys = peoplePicker.GetAllUserKeys();
var finalusers = new Array();
for (var i = 0; i < users.length; i++) {
    var arrayuser = users[i];
    finalusers.push(SP.FieldUserValue.fromUser(arrayuser.Key));
}
listItem.set_item("PeopleColumnInternalName", finalusers);
listItem.update();
ctx.load(listItem);

ctx.executeQueryAsync(function(){
    console.log("inserted successfully");
}, function(){
    console.log("something went wrong");
});

因此,您可以稍微修改一下代码。替换为您的列表名称和用户列的内部名称:

function getUserInfo(PeoplepickerId) {  
    var UsersID="";
    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict[PeoplepickerId + "_TopSpan"];  

        if (peoplePicker.HasInputError) return false; // if any error  
        else if (!peoplePicker.HasResolvedUsers()) return false; // if any invalid users  
        else if (peoplePicker.TotalUserCount > 0) {  
            // Get information about all users.  
            var users = peoplePicker.GetAllUserInfo();  
            //var userInfo = '';  
            //var promise = '';  
             for (var i = 0; i < users.length; i++) {  
                 UsersID += users[i].DisplayText + "\n";
                 UsersID += users[i].EntityData.Email;
             }

            //insert data to sharepoint
            var ctx = new SP.ClientContext.get_current();

            var list = web.get_lists().getByTitle("Custom List");
            var listCreationInformation = new SP.ListItemCreationInformation();
            var listItem = list.addItem(listCreationInformation);           

            // Get user keys.
            var keys = peoplePicker.GetAllUserKeys();
            var finalusers = new Array();
            for (var i = 0; i < users.length; i++) {
                var arryuser = users[i];
                finalusers.push(SP.FieldUserValue.fromUser(arryuser.Key));
            }
            listItem.set_item("PeopleColumnInternalName", finalusers);
            listItem.update();
            ctx.load(listItem);

            ctx.executeQueryAsync(function(){
                console.log("inserted successfully");
                return UsersID;
            }, function(){
                console.log("something went wrong");
                return UsersID;
            });

        }       
}