c# - 使用客户端对象模型更新 SharePoint 中的字段值

标签 c# sharepoint field sharepoint-clientobject

所以我正在尝试创建一种主要用于更改 SharePoint 中字段值的方法。

这就是我目前所拥有的......

static String fieldName1 = "Title";
static String fieldName2 = "Keywords";
static String title = "A Beautiful Sunset";
static String keywords1 = "test1";
static String keywords2 = "test2";
static String keywords3 = "test3";

static NetworkCredential credentials = new NetworkCredential(username, password, domain);
static ClientContext clientContext = new ClientContext(URL);
static Web site = clientContext.Web;
static List list = site.Lists.GetByTitle(listName);
static FileCreationInformation newFile = new FileCreationInformation();

private static void updateFields()
{
    clientContext.Load(list);
    FieldCollection fields = list.Fields;
    clientContext.Load(fields);
    clientContext.Load(list.RootFolder);

    ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();

    foreach (var listItem in listItems)
    {
        Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
        clientContext.Load(listItem.File);
        clientContext.ExecuteQuery();
        Console.WriteLine("listItem File Name: {0}", listItem.File.Name);

        if (listItem.File.Name.Contains("Sunset"))
        {
            ////????????
        }
        listItem.Update();
    }
    clientContext.ExectueQuery();
}

我知道如何访问该字段,但我不确定如何访问该字段中的实际值并对其进行修改。有没有人有使用客户端对象模型的经验?感谢您提供的任何帮助!

最佳答案

使用客户端对象模型更新字段非常简单:

ClientContext ctx = new ClientContext("http://yoursite");
List list = ctx.Web.Lists.GetByTitle("ListName");
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); // loading all the fields
ctx.ExecuteQuery();

foreach(var item in items)
{
    // important thing is, that here you must have the right type
    // i.e. item["Modified"] is DateTime
    item["fieldName"] = newValue;

    // do whatever changes you want

    item.Update(); // important, rembeber changes
}
ctx.ExecuteQuery(); // important, commit changes to the server

对于 DocumentLibrary,这是完全不同的 - 您可以获得相同的 ListItem 对象,但要访问关联的文件,您必须使用 item.File 属性。所以 ListItem 本身将包含字段值,listItem.File 将包含文件,比如图像。
不要忘记 - 要访问该文件,您必须 Load() 然后 ExecuteQuery()

关于c# - 使用客户端对象模型更新 SharePoint 中的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6947491/

相关文章:

javascript - Asp.net 将多个参数传递给 javascript 函数

c# - 子串匹配索引

c# - 使用 VALUE 作为单元格颜色

sharepoint - 将JSP转换为SharePoint Webpart

c# - 使用 Properties 而不是私有(private)字段和 get/set 方法有什么优势?

vb.net - VB.Net-无法在Access中创建字段 “password”

c# - 如何在 C# 中 DllImport 枚举

sharepoint - 在 Teams 团队生成的网站上的 Sharepoint Online 中创建子网站

c# - SharePoint CAML 查询在生成器中有效,但在代码中无效

mysql - 如何使mysql字段唯一?