所以我正在尝试创建一种主要用于更改 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/