将 SharePoint 用户写入 SharePoint 列表中的用户字段的正确方法 [英] The proper way to write a SharePoint User to a User Field in a SharePoint list

查看:57
本文介绍了将 SharePoint 用户写入 SharePoint 列表中的用户字段的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将用户写入 SharePoint 列表.

I'm writing a user to a SharePoint list.

我读到 SharePoint 用户字段中有这样的字符串:userId;#userLoginName

I've read that the SharePoint User field has a string like this inside of it: userId;#userLoginName

在写入用户字段时,我尝试以相同的方式进行格式化,例如,当我编写此字符串时,它可以工作:9;#i:0#.f|membership|ectropy@example.org

I've tried formatting in the same way when writing to a User field, for example when I write this string it works: 9;#i:0#.f|membership|ectropy@example.org

但奇怪的是(至少对我而言)9;# 似乎工作,甚至 9.即使我根本不传递 userLoginName 信息,loginId 似乎也足以让它识别出我在谈论哪个用户.

But what is strange (to me at least) is that 9;# seems to work or even 9. Even if I don't pass the userLoginName info at all, the loginId seems to be enough for it to recognize which User I'm talking about.

这似乎意味着在写入 SharePoint 用户字段时,您只需要 id 和 userLoginName,或者实际上,loginId 之后的所有内容都无关紧要.

This seems to imply that when writing to the SharePoint User field, you only need the id, and the userLoginName, or, indeed, everything after the loginId is irrelevant.

我的推理是否正确?或者,如果我不提供 userLoginName 信息,可能会产生意想不到的后果?

Am I correct in my reasoning here? Or perhaps there's unexpected consequences if I leave off the userLoginName information?

推荐答案

您的假设是正确的,在为 User 字段指定值时,只有 User Id 是必需属性.

Your assumption is correct, only User Id is a mandatory property when specifying value for a User field.

但由于 SP.FieldUserValue 对象是用于存储 User 字段的值,建议使用此对象获取和设置值,如下例所示:

But since SP.FieldUserValue object is used to store value of User field, it is recommended to get and set values using this object as demonstrated in the below example:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getByTitle(listTitle);
var item = list.getItemById(itemId);

var assignedToVal = new SP.FieldUserValue();
assignedToVal.set_lookupId(11);   //specify User Id 
item.set_item(fieldName,assignedToVal);
item.update();

ctx.executeQueryAsync(
    function() {
        console.log('Updated');
    },
    function(sender,args) {
        console.log('An error occurred:' + args.get_message());
    }
);

这篇关于将 SharePoint 用户写入 SharePoint 列表中的用户字段的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆