首页 > 解决方案 > 如何使用 .Net 更新 Sharepoint ListItem?

问题描述

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

这是我正在运行的一段代码,它不会更新 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();

    }
}

标签: c#.netsharepointsharepoint-2013splistitem

解决方案


文档的问题在于它没有明确指出您必须使用 .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();

                    }
            }

推荐阅读