Forum Discussion

john john's avatar
john john
Steel Contributor
Oct 02, 2020

CSOM code to create a new item and its versions based on another item and items versions

I have the following CSOM code, to copy list items from one list to another:-

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

context.Load(collListItem,
items => items.Include(
item => item["Title"],
item => item["CustomerName"],
item => item["SupplierName"],
item => item["Currency"],
item => item["Payment_x0020_Period"],
item => item["PaymentAmount"],
item => item["ContractValue"],
item => item["NoticePeriodInMonths"],
item => item["Condition_x0020_of_x0020_Termina"],
item => item["DepartmentResponsible"],
item => item["V3Comments"],
item => item["businessfunctionsInitials"]));
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["Currency"] = item["Currency"];
                        listItem2["Payment_x0020_Period"] = item["Payment_x0020_Period"];
                        listItem2["PaymentAmount"] = item["PaymentAmount"];
                        listItem2["ContractValue"] = item["ContractValue"];
                        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"];
                        listItem2.SystemUpdate();
                        context.ExecuteQuery();



                    }

 

The 2 lists share the same content type, so my above code worked well, but did not copy the versions, so how i can modify my above code to copy the items versions?

Thanks

No RepliesBe the first to reply

Resources