SharePoint 客户端对象模型修改默认字段

  最近,在做SharePoint 迁移相关的工作,需要先迁移基本字段信息,最后修改默认字段(创建时间/修改时间/修改者/创建者);

ClientContext ctx = new ClientContext("http://siteURL");
List list = ctx.Web.Lists.GetByTitle("ListName");
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items); //loading all the fields
ctx.ExecuteQuery();
User theUser = ctx.Web.EnsureUser("domain\\user");
ctx.Load(theUser);
ctx.ExecuteQuery();
foreach(var item in items) {
    item["Created"] = "2015-07-03";
    item["Modified"] = "2017-07-03";
    item["Editor"] = theUser;
    item["Author"] = theUser;
    item.Update(); //Apply the change
}
ctx.ExecuteQuery();

  特别要注意的是:

  1. 因为是客户端对象模型,每次EnsureUser之后,都需要Load和ExecuteQuery以后,才能获取到用户;
  2. 修改完基本字段,最后更新默认字段;
posted @ 2021-02-13 13:02  霖雨  阅读(87)  评论(0编辑  收藏  举报