简体   繁体   English

使用 csom c# 更新 sharepoint 中的“创建者”

[英]update “created by” in sharepoint using csom c#

I want to update "created by" column in sharepoint.我想更新 sharepoint 中的“创建者”列。 For this, i have to do using CSOM only as i do not have sharepoint server dll.为此,我只需要使用 CSOM,因为我没有 sharepoint 服务器 dll。 I wrote code in this...我在这写了代码...

ClientContext cc = new ClientContext ("http://sharepoint...");
List list = cc.web.Lists.GetByTitle("sharepointlistname");
cc.Load(list);
ListItem item = list.GetItemById(13);//In this case, i want to update only id=13. I want to know code for all records also.

cc.Load(item);
cc.ExecuteQuery();
item("Created by") = "Dinesh";
item.Update();

When I run above code, am getting this error...当我运行上面的代码时,我得到了这个错误......

sharepoint list Invalid data has been used to update the list item. sharepoint 列表 无效数据已用于更新列表项。 The field you are trying to update may be read only您尝试更新的字段可能是只读的

The Created By field internal name is Author and it's a Person field, please update like this: Created By 字段内部名称是 Author 并且它是一个 Person 字段,请像这样更新:

    ClientContext ctx = new ClientContext("http://sp/sites/dev");
    List list = ctx.Web.Lists.GetByTitle("MyList");
    Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
  //CamlQuery to filter items which created in Today  
  camlQuery.ViewXml =
       @"<View>  
    <Query> 
       <Where><Eq><FieldRef Name='Created' /><Value Type='DateTime'><Today /></Value></Eq></Where> 
    </Query> 
    </View>";  
    ListItemCollection items = list.GetItems(camlQuery);
    ctx.Load(items);
    ctx.ExecuteQuery();

    User theUser = ctx.Web.EnsureUser("Contoso\\Jerry");
    ctx.Load(theUser);
    ctx.ExecuteQuery();

    foreach (var item in items)
    {
        item["Editor"] = theUser;
        item["Author"] = theUser;
        item.Update();
    }
    ctx.ExecuteQuery();

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM