0

I have the following code to copy a list item to another list:-

List oList = context.Web.Lists.GetByTitle("Supplier Contracts");
                    List oList2 = context.Web.Lists.GetByTitle("Finance Leases");
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
                    ListItemCollection collListItem = oList2.GetItems(camlQuery);

                    context.Load(collListItem,
items => items.Include(
item => item["Title"],
item => item["CustomerName"],
item => item["SupplierName"],
item => item["Supplier_x0020_Product_x0020_Des"],
item => item["Contract_x0020_Number"],
item => item["Contract_x0020_Status"],
item => item["Currency"],
item => item["Payment_x0020_Period"],
item => item["PaymentAmount"],
item => item["ContractValue"],
item => item["Contract_x0020_Start_x0020_date"],
item => item["Contract_x0020_End_x0020_date"],
item => item["Contract_x0020_Review_x0020_Date"],
item => item["NoticePeriodInMonths"],
item => item["Condition_x0020_of_x0020_Termina"],
item => item["DepartmentResponsible"],
item => item["V3Comments"],
item => item["businessfunctionsInitials"]));

                    // List wikiList = context.Web.Lists.GetByTitle("Customer Service KB");
                    context.Load(collListItem);
                    context.Load(context.Site);
                    context.ExecuteQuery();
                    foreach (ListItem item in collListItem)
                    {
                        ListItemCreationInformation itemCreateInfo2 = new ListItemCreationInformation();
                        ListItem listItem2 = oList.AddItem(itemCreateInfo2);
                        listItem2["Title"] = item["Title"];
                        listItem2["CustomerName"] = item["CustomerName"];
                        listItem2["SupplierName"] = item["SupplierName"];
                        listItem2["Supplier_x0020_Product_x0020_Des"] = item["Supplier_x0020_Product_x0020_Des"];
                        listItem2["Contract_x0020_Number"] = item["Contract_x0020_Number"];
                        listItem2["Contract_x0020_Status"] = item["Contract_x0020_Status"];
                        listItem2["Currency"] = item["Currency"];
                        listItem2["Payment_x0020_Period"] = item["Payment_x0020_Period"];
                        listItem2["PaymentAmount"] = item["PaymentAmount"];
                        listItem2["ContractValue"] = item["ContractValue"];
                        listItem2["Contract_x0020_Start_x0020_date"] = item["Contract_x0020_Start_x0020_date"];
                        listItem2["Contract_x0020_End_x0020_date"] = item["Contract_x0020_End_x0020_date"];
                        listItem2["Contract_x0020_Review_x0020_Date"] = item["Contract_x0020_Review_x0020_Date"];
                        listItem2["NoticePeriodInMonths"] = item["NoticePeriodInMonths"];
                        listItem2["Condition_x0020_of_x0020_Termina"] = item["Condition_x0020_of_x0020_Termina"];
                        listItem2["DepartmentResponsible"] = item["DepartmentResponsible"];
                        listItem2["V3Comments"] = item["V3Comments"];
                        listItem2["businessfunctionsInitials"] = item["businessfunctionsInitials"];

                        //context.Load(context.Web);
                        //context.ExecuteQuery();
                        //NewTryToAttachFiles(context,context.Web, oList2,item,oList,listItem2);
                        listItem2.SystemUpdate();
                        context.ExecuteQuery();



                    }

but the listItem2.SystemUpdate(); will raise this error:-

Operation is not valid due to the current state of the object

1 Answer 1

0

Does SystemUpdate() exist for ListItem? I don't see it.

Reading the following at Client Object Model SystemUpdate

Client Object Model have the limitations with regards to the way a List Item Update works. More specifically there is no SystemUpdate() type functionality available in Client Object Model, like in the 'full' SharePoint Object Model. If you really want to develop his functionality, then write your own custom web service that updates the document metadata using SystemUpdate so a new version isn't created and deploy it to your SharePoint Server.

Update() worked for me like in the following code

  string sitrUrl = "http://SharePoint.com/sites/MySite";

            using (var context = new ClientContext(sitrUrl))
            {
                //Source
                List oList2 = context.Web.Lists.GetByTitle("TestAuthor");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>2</Value></Eq></Where></Query></View>";
                ListItemCollection collListItem = oList2.GetItems(camlQuery);

                context.Load(collListItem,
                items => items.Include(
                item => item["Title"],
                item => item["Status"]
                ));

                context.ExecuteQuery();

                //Destination List
                List oList = context.Web.Lists.GetByTitle("Test1");

                foreach (ListItem item in collListItem)
                {
                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    ListItem oListItem = oList.AddItem(itemCreateInfo);
                    oListItem["Title"] = item["Title"];
                    oListItem["Status"] = item["Status"];
                    oListItem.Update();
                    
                    context.ExecuteQuery();
                }
            }

2
  • recently SystemUpdate() became available inside CSOM!!
    – John John
    Oct 8, 2020 at 18:55
  • Oh, that might be the case. However, when you're adding a new item to a list, why do you need SytemeUpdate()? The only difference between Update() and this is that it would not update Modified Date and Modified By Oct 8, 2020 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.