使用客户端对象模型将用户添加到 UserMulti 字段类型

发布于 2025-01-08 10:30:32 字数 477 浏览 2 评论 0 原文

我是 SharePoint 新手,所以请耐心等待。

我需要能够使用客户端对象模型在我们的自定义列表中创建一个新的列表项。我一直在关注 MSDN 网站 上描述的示例,并且大多数情况下这已经奏效了。

我们有一个包含多个字段的列表,其中包括 UserMulti 字段类型。我在将用户添加到该字段时遇到问题。到目前为止,我已经尝试了以下操作,但这似乎总是默认为系统帐户而不是字段中指定的用户。

   ...
   listItem["ProjectMembers"] = "1;#domain\\johndoe";
   listItem.Update();
   _clientContext.ExecuteQuery();

我需要先进行某种类型的查找吗?任何帮助表示赞赏。谢谢。

I'm bit of a SharePoint noobie so please bear with me.

I need to be able to create a new list item in one our custom list using the client object model. I have been following the example described on the MSDN site and for the most part this has worked.

We have a list that contains several fields including a UserMulti field type. I am having problems adding users to this field. So far I have tried the following but this somehow always seems to default to the system account rather than the user specified in the field.

   ...
   listItem["ProjectMembers"] = "1;#domain\\johndoe";
   listItem.Update();
   _clientContext.ExecuteQuery();

Do I need to do some type of lookup first? Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2025-01-15 10:30:32

我花了一点时间,但最终我想通了。以下是您可以采取的两种方法

  1. 为列表项分配委托人

     varprincipal = _rootWeb.EnsureUser("domain\\johndoe") asprincipal;
        listItem["ProjectMembers"] = 主体;
        listItem.Update();
        _clientContext.ExecuteQuery();
    
  2. 如果您需要为字段分配多个用户,请分配 FieldUserValue 列表。

     string[] users = { "domain\\johndoe", "domain\\peterpan" };
        var 项目成员 = 用户
            .Select(loginName => FieldUserValue.FromUser(loginName))
            .ToList();
    
        listItem["ProjectMembers"] = projectMembers;
        listItem.Update();
        _clientContext.ExecuteQuery();
    

我确信有更好的方法来做事,您可以将两者结合起来以确保用户在将其添加到列表之前是有效的,但到目前为止这是有效的。希望这对其他人有帮助。

It took a little while but I figured it out in the end. Below are two approaches you can take

  1. Assign a Principal to the list item

        var principal = _rootWeb.EnsureUser("domain\\johndoe") as Principal;
        listItem["ProjectMembers"] = principal;
        listItem.Update();
        _clientContext.ExecuteQuery();
    
  2. Assign an list of FieldUserValue if you need to assign more than one user to the field.

        string[] users = { "domain\\johndoe", "domain\\peterpan" };
        var projectMembers = users
            .Select(loginName => FieldUserValue.FromUser(loginName))
            .ToList();
    
        listItem["ProjectMembers"] = projectMembers;
        listItem.Update();
        _clientContext.ExecuteQuery();
    

I'm sure there's better ways of doing things and you could combine the two to ensure that the users are valid before adding them to the list, but this is working so far. Hope this help someone else.

云淡月浅 2025-01-15 10:30:32

Microsoft 有一篇 wiki 文章“SharePoint:使用 C# 获取和设置字段的完整指南”可以提供帮助。 http://social.technet.microsoft.com/wiki/contents/articles/21801.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-c.aspx# Set_and_Get_a_Multi-Person_Field

它包含此示例代码。

var lotsofpeople = new SPFieldUserValueCollection(web, item["lotsofpeoplefield"].ToString());
var personA = web.EnsureUser("contoso\\fred");
var personAValue = new SPFieldUserValue(web, personA.ID, personA.LoginName);
var personB = web.EnsureUser("contoso\\barnie");
var personBValue = new SPFieldUserValue(web, personB.ID, personB.LoginName);
lotsofpeople.Add(personAValue);
lotsofpeople.Add(personBValue);
item["lotsofpeoplefield"] = lotsofpeople;
item.Update();

Microsoft has a wiki article, "SharePoint: A Complete Guide to Getting and Setting Fields using C#" that can help. http://social.technet.microsoft.com/wiki/contents/articles/21801.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-c.aspx#Set_and_Get_a_Multi-Person_Field

It includes this sample code.

var lotsofpeople = new SPFieldUserValueCollection(web, item["lotsofpeoplefield"].ToString());
var personA = web.EnsureUser("contoso\\fred");
var personAValue = new SPFieldUserValue(web, personA.ID, personA.LoginName);
var personB = web.EnsureUser("contoso\\barnie");
var personBValue = new SPFieldUserValue(web, personB.ID, personB.LoginName);
lotsofpeople.Add(personAValue);
lotsofpeople.Add(personBValue);
item["lotsofpeoplefield"] = lotsofpeople;
item.Update();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文