c# - 以编程方式将用户添加到列表

标签 c# sharepoint sharepoint-list


我有一个列表,该列表是从接受用户输入的程序中填充的,在将个人或组添加到列表中的其中一列时遇到问题。有人知道怎么做吗?

最佳答案

看看这篇文章:http://blogs.msdn.com/b/uksharepoint/archive/2009/02/17/quick-tip-using-the-sharepoint-person-or-group-field-in-code-part-1.aspx

它向您展示了如何使用它:

Console.WriteLine("Enter a ; delimited list of domain\alias that need to be added:");
string sAliases = Console.ReadLine(); //captures whatever the user entered
string sValueToAddToFieldInSP = ""; //used to build the full string needed for the person field

string sAllContacts = "";

using (SPSite site = new SPSite(“http://sites/site/yoursite”))
{
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.RootWeb)
    {
        web.AllowUnsafeUpdates = true;
        string[] aAliases = sAliases.Split(';');
        foreach (string sAlias in aAliases)
        {
            SPUser user = web.EnsureUser(sAlias);
            sAllContacts += user.ID.ToString() + ";#" + user.LoginName.ToString() + ";#";
        }
        web.Update();
    }
}

if (sAllContacts.EndsWith(";#"))
{
    sAllContacts = sAllContacts.Substring(0, sAllContacts.Length - 2);
}

//add the list item
SPList l = web.Lists["<name of your list>"];
SPListItem li= l.Items.Add();
li["Title"] = sAllContacts ;
li["MyPerson"] = sAllContacts ;
li.Update();
Console.WriteLine("Done");

关于c# - 以编程方式将用户添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4124297/

相关文章:

c# - 如何使用 csom 将 SharePoint 2013 列表数据绑定(bind)到 asp.net 的 gridview 中的下拉列表

sharepoint-2010 - 如何限制列表中的项目数?

sharepoint - 如何在共享点列表的编辑表单中将 ID 字段显示为只读?

c# - 什么是显示松耦合类和接口(interface)之间关系的好方法?

c# - 从字符串中获取枚举值

excel - 将共享点列表与多个 Excel 文件同步

r - 使用 R 连接到共享点列表

c# - Resharper:在调试期间显示表达式值

c# - 如何防止表单对象在关闭时被释放?

Sharepoint:查看当前用户是否属于指定组