I was in a scenario where I had to do a add some data to a document list in a web job. My issue was that I could not find any system update method in the CSOM API. After searching it i found a blog post showing that one could just override the system fields, like Modified and Editor(Modified My). That seemed to work but that did not stop it form updating the version number of the document.
This example will only work for minor unpublished version. I have not found a way to do this without a published version. There it will get a new minor version.
The solution combines the use of adding Editor, and Modified with the use of the ValidateUpdateListItem metod. I found that one can set the item values on the item and just set one value in the form value list, then it will update all the fields. If one then set the "bNewDocumentUpdate" it will not update the version number if it is a minor version.
So what i do is just get the values from the item before an update, then sets the value back on the item. Then execute the ValidateUpdateListItem.
Here is some code, i just modify the title to see that the value is set.
// get the item from the list
....
var editor = item["Editor"];
var modified = item["Modified"];
// add the values back to the item
item["Editor"] = editor;
item["Modified"] = modified;
// this is needed to make sure the ValidateUpdateListItem does the job
var titleUpdate = new ListItemFormUpdateValue
{
FieldName = "Title",
FieldValue = "Test" + DateTime.Now.Ticks.ToString()
};
updates.Add(titleUpdate);
item.ValidateUpdateListItem(updates, true, "");
clientContext.ExecuteQuery();
This example will only work for minor unpublished version. I have not found a way to do this without a published version. There it will get a new minor version.
The solution combines the use of adding Editor, and Modified with the use of the ValidateUpdateListItem metod. I found that one can set the item values on the item and just set one value in the form value list, then it will update all the fields. If one then set the "bNewDocumentUpdate" it will not update the version number if it is a minor version.
So what i do is just get the values from the item before an update, then sets the value back on the item. Then execute the ValidateUpdateListItem.
Here is some code, i just modify the title to see that the value is set.
// get the item from the list
....
var editor = item["Editor"];
var modified = item["Modified"];
// add the values back to the item
item["Editor"] = editor;
item["Modified"] = modified;
// this is needed to make sure the ValidateUpdateListItem does the job
var titleUpdate = new ListItemFormUpdateValue
{
FieldName = "Title",
FieldValue = "Test" + DateTime.Now.Ticks.ToString()
};
updates.Add(titleUpdate);
item.ValidateUpdateListItem(updates, true, "");
clientContext.ExecuteQuery();
Comments