来自人员字段的SharePoint 2013 JSOM用户密钥

时间:2013-07-17 07:06:37

标签: javascript sharepoint sharepoint-2013 csom

有人可以帮助我使用javascript从人员列中获取用户信息吗?到目前为止,我已经能够读取列表项并返回一个SP.FieldUserValue,我可以从中获取数字ID(不确定此ID是什么)和显示名称。 e.g。

var ManVal  = oListItem.get_item("RecruitingManager").get_lookupValue();
var ManId   = oListItem.get_item("RecruitingManager").get_lookupId();

如何更进一步创建sp用户对象?

最终,我想要实现的是从列表中检索详细信息,然后填充人物编辑器。

2 个答案:

答案 0 :(得分:3)

好的,我知道了。

这是我的代码,希望它对某人有所帮助。我没有包括检索列表项的方法,只是来自该函数的行,我得到了该人的值。

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

答案 1 :(得分:0)

person字段(实际上称为“人员选择器”)具有您可能会觉得有用的特定JavaScript函数:GetAllUserInfo()

MSDN上有一篇很好的文章: How to: Use the client-side People Picker control in apps for SharePoint

相关代码是:

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

所以基本上你必须将你的字段转换为SPClientPeoplePicker,然后可以使用GetAllUserInfo来迭代字段中的所有用户。