如何使用 .Net 更新 Sharepoint ListItem?

How do I update a Sharepoint ListItem with .Net?

基本上我查询 Sharepoint 并获得 ListItems 列表。我 foreach 遍历列表并检查是否需要从外部数据库更新该项目(该代码不存在)

这是我 运行 不会更新 Sharepoint ListItem 的代码。我什至尝试了不同的凭证都无济于事。

using(ClientContext ctx = new ClientContext(searchsiteurl)) {
    //NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
    //ctx.Credentials = credit;
    Web web = ctx.Web;

    List list = web.Lists.GetById(new Guid(site.ListGUID));

    var q = new CamlQuery();
    if (Fullsync) {
        q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
    }
    else {

        q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" + "<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" + "</And></And></Where></Query></View>";
    }

    var r = list.GetItems(q);
    ctx.Load(r);
    ctx.ExecuteQuery();
    foreach(SP.ListItem lit in r) {
        //do a whole bunch of stuff....

        // this does NOT WORK
        lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
        lit.Update();
        ctx.ExecuteQuery();

    }
}

文档的问题在于它没有明确指出您必须使用 .GetItemById() 函数来检索 ListItem 以更新该 ListItem。

所以这里的代码应该可以帮助未来的人们搜索这个答案。我花了很长时间才弄明白。

                 using (ClientContext ctx = new ClientContext(searchsiteurl))
                {
                    //NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
                    //ctx.Credentials = credit;
                    Web web = ctx.Web;

                    List list = web.Lists.GetById(new Guid(site.ListGUID));


                   var q = new CamlQuery();
                    if (Fullsync)
                    {
                        q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" +
                            "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
                    }
                    else
                    {

                        q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" +
                                "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" +
                                "<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" +
                                "</And></And></Where></Query></View>";
                    }


                    var r = list.GetItems(q);
                    ctx.Load(r);
                    ctx.ExecuteQuery();
                    foreach (SP.ListItem lit in r)
                    {
                                //do a whole bunch of stuff....


                                /* this does NOT WORK
                                lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
                                lit.Update();
                                ctx.ExecuteQuery();
                                */

                                // this works!
                                    var KAToModify = list.GetItemById(lit.Id);
                                    KAToModify["Linked_x0020_CSRs"] = LinkedSRs;
                                    KAToModify.Update();
                                    ctx.ExecuteQuery();

                    }
            }

它不会工作你可以使用这个枚举器方法

enter code here

 foreach (var i in cv)
            {
               var items = new ListItemCreationInformation();
               var item = lists.AddItem(items);
                item["Title"] = i.Title;

item.Update();
                    ctx.Load(item);
                    ctx.ExecuteQuery();

    }`

你会得到你所有的列表项 如果你想要列表中的特定项目 你使用 GetById()