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

前端之家收集整理的这篇文章主要介绍了c# – 使用客户端对象模型在SharePoint中更新字段值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我试图创建一个主要用于更改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().

原文链接:https://www.f2er.com/csharp/101134.html

猜你在找的C#相关文章