我正在尝试使用 Javascript 填充 Sharepoint 2013 中的 PeoplePicker 控件:
name="Engineer"
var ppDiv=$("[id$='ClientPeoplePicker'][title='"+name+"']");
console.log(ppDiv.html());
var ppEditor=ppDiv.find("[title='"+name+"']");
var spPP=SPClientPeoplePicker.SpClientPeoplePickerDict[ppDiv[0].id];
ppEditor.val("Abc, Xyz");
spPP.AddUnresolvedUserFromEditor(true);
两个问题:赋值语句后
ppDiv
仍然未定义,SPClientPeoplePicker
未定义;根据这篇文章,它是由clientpeoplepicker.js
定义的,这是一个应该加载到每个具有人员选择器的页面上的脚本。
编辑:
我将代码更改为:
name="Engineer"
var ppDiv=$("div[title='"+name+"']");
console.log(ppDiv.html());
var ppEditor=ppDiv.find("[title='"+name+"']");
var spPPD=SPClientPeoplePicker.SpClientPeoplePickerDict;
console.log(spPPD[0]);
var spPP=spPPD[ppDiv[0].id];
ppEditor.val("Abc, Xyz");
spPP.AddUnresolvedUserFromEditor(true);
现在 ppDiv 已定义(如其 HTML 成功出现在控制台日志中所示),但它在
var spPPD=SPClientPeoplePicker.SpClientPeoplePickerDict;
线上崩溃并出现以下错误:
Uncaught ReferenceError: SPClientPeoplePicker is not defined
我是这样做的。 fieldName 是人员选择器的列标题,userAccountName 是用户登录名/声明的最后部分。
需要指出的一点是,它必须是客户人员选择器。据我所知,sharepoint 默认情况下以自定义形式使用服务器端人员选择器,因此请确保它确实是客户端人员选择器。
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
var controlName = fieldName;
var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
peoplePickerEditor.val(userAccountName);
spPeoplePicker.AddUnresolvedUserFromEditor(true);
//disable the field
spPeoplePicker.SetEnabledState(false);
//hide the delete/remove use image from the people picker
$('.sp-peoplepicker-delImage').css('display','none');
}
我发现如果我在调用代码之前添加 500-700 毫秒的延迟,我就不会收到此错误:
window.setTimeout(function() {
SetAndResolvePeoplePicker("PickerFieldName", "[email protected]");
}, 750);
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
var controlName = fieldName;
var peoplePickerDiv = jQuery("[id$='ClientPeoplePicker'][title='" + controlName + "']");
var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
peoplePickerEditor.val(userAccountName);
spPeoplePicker.AddUnresolvedUserFromEditor(true);
}