如何使用.Net更新Sharepoint ListItem?

时间:2018-05-08 13:48:52

标签: c# .net sharepoint sharepoint-2013 splistitem

基本上我查询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();

    }
}

2 个答案:

答案 0 :(得分:0)

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

                    }
            }

答案 1 :(得分:0)

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

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()