javascript 来自人员字段的 SharePoint 2013 JSOM 用户密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17693335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharePoint 2013 JSOM User Key From Person Field
提问by Phillip Hill
Can anyone help me to get the user info from a person column using javascript? So far I have been able to read the list item and return a SP.FieldUserValue from which I can get a numeric Id (not sure what this ID is) and the display name. e.g.
任何人都可以帮助我从使用 javascript 的人员列中获取用户信息吗?到目前为止,我已经能够读取列表项并返回一个 SP.FieldUserValue,从中我可以获得一个数字 Id(不确定这个 ID 是什么)和显示名称。例如
var ManVal = oListItem.get_item("RecruitingManager").get_lookupValue();
var ManId = oListItem.get_item("RecruitingManager").get_lookupId();
How do I take this one step further to create a sp user object?
如何更进一步创建 sp 用户对象?
Ultimately what I'm trying to achieve is to retrieve the details from the list and then populate a people editor.
最终我想要实现的是从列表中检索详细信息,然后填充人员编辑器。
回答by Phillip Hill
Ok, I've got it.
好的,我知道了。
Here is my code, hope it helps somebody. I haven't included the method to retrieve the list item, just the line from that function where I'm getting the value of the person.
这是我的代码,希望它可以帮助某人。我没有包括检索列表项的方法,只是从该函数中获取该人的价值的行。
var _lineManager;
var lineManager = oListItem.get_item("RecruitingManager").get_lookupId();
_lineManager = oWebsite.getUserById(lineManager);
getLineManager();
function getLineManager() {
context.load(_lineManager);
context.executeQueryAsync(onGetUserNameSuccessLM, onGetUserNameFailLM);
}
function onGetUserNameSuccessLM() {
alert(lineManager.get_title());
var schema = {};
schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
schema['SearchPrincipalSource'] = 15;
schema['ResolvePrincipalSource'] = 15;
schema['AllowMultipleValues'] = false;
schema['MaximumEntitySuggestions'] = 50;
schema['Width'] = '280px';
var users = new Array(1);
var defaultUser = new Object();
defaultUser.AutoFillDisplayText = lineManager.get_title();
defaultUser.AutoFillKey = lineManager.get_loginName();
defaultUser.Description = lineManager.get_email();
defaultUser.DisplayText = lineManager.get_title();
defaultUser.EntityType = "User";
defaultUser.IsResolved = true;
defaultUser.Key = lineManager.get_loginName();
defaultUser.Resolved = true;
users[0] = defaultUser;
SPClientPeoplePicker_InitStandaloneControlWrapper('peoplePickerDivLinMan', users, schema);
}
function onGetUserNameFailLM(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
回答by Dennis G
The person field (actually called "people picker") has a specific JavaScript function which you might find useful: GetAllUserInfo()
person 字段(实际上称为“人员选择器”)有一个特定的 JavaScript 函数,您可能会发现它很有用: GetAllUserInfo()
There is a nice article on MSDN: How to: Use the client-side People Picker control in apps for SharePoint
MSDN 上有一篇不错的文章: How to: Use the client-side People Picker control in apps for SharePoint
The relevant code is:
相关代码是:
// Get the people picker object from the page.
var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
var userInfo = '';
for (var i = 0; i < users.length; i++) {
var user = users[i];
for (var userProperty in user) {
userInfo += userProperty + ': ' + user[userProperty] + '<br>';
}
}
$('#resolvedUsers').html(userInfo);
// Get user keys.
var keys = peoplePicker.GetAllUserKeys();
$('#userKeys').html(keys);
So basically you have to cast your field to a SPClientPeoplePicker
and can then use GetAllUserInfo
to iterate over all users in the field.
所以基本上你必须将你的字段转换为 a SPClientPeoplePicker
,然后可以GetAllUserInfo
用来迭代该字段中的所有用户。