How do I update a Sharepoint ListItem with .Net?

765 views Asked by At

Basically I query Sharepoint and get a list of ListItems. I foreach through the list and check to see if the item needs to be updated from an external db (that code is not present)

Here is the bit of code that I was running that would not update the Sharepoint ListItem. I even tried different credentials to no avail.

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

There are 2 answers

0
LeeFranke On BEST ANSWER

The problem with the documentation is that it does not explicitly indicate that you must use the .GetItemById() function to retrieve a ListItem in order to update that ListItem.

So here is the code that should help out future people searching for this answer. It took me waaaay too long to figure this out.

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

                    }
            }
0
Md Mus On

it Won`t work you Can use this Enumerator Method

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

    }`

you will get all your list item you this if you want particular item in list you use GetById()