Basically, when we are trying to update a SharePoint List Item/Document then the item/file version is increased according to the list versioning. In the Server side object model, we can update a List Item without increasing its file version by setting ‘SystemUpdate’ as false. But in CSOM ‘SystemUpdate’ property is not supported.
In this below code block, I have mentioned how we can update a Listitem/file without changing item/file version number by using “ValidateUpdateListItem” method.
I have used a sample console application to achieve the goal. I have executed the below code sample for an SPO (O365) server and this will also work for all other SharePoint server versions except SP 2010 server.
In the code sample, I have updated the ‘Modified User’ and ‘Modified Date’ field of a SharePoint document.
The code block for this is as mentioned below,
- using System;
- using System.Security;
- using System.Collections.Generic;
- using Microsoft.SharePoint.Client;
- namespace ConsoleApp1 {
- class Program {
- static void Main(string[] args) {
- List list = null;
- Web web = null;
- User modifiedBy = null;
- ListItem item = null;
- ClientContext context = null;
- ListItemCollection itemCollection = null;
- SecureString pswd = new SecureString();
- string userName = string.Empty;
- string password = string.Empty;
- string webUrl = string.Empty;
- string modifiedDate = string.Empty;
- try {
-
- webUrl = "https://SiteUrl";
- using(context = new ClientContext(webUrl)) {
- userName = "UserName";
- password = "Password";
- foreach(char c in password.ToCharArray())
- pswd.AppendChar(c);
-
- context.Credentials = new SharePointOnlineCredentials(userName, pswd);
- web = context.Web;
- context.Load(web, w => w.ServerRelativeUrl, w => w.Id, w => w.CurrentUser);
- context.ExecuteQuery();
-
- list = context.Web.Lists.GetByTitle("Documents");
- context.Load(list, L => L.Id);
-
- itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
-
- context.Load(itemCollection, IC => IC.Include(I => I.Id, I => I.DisplayName, I => I.File, I => I.File.ServerRelativeUrl));
- context.ExecuteQuery();
-
- modifiedDate = "20/01/2019";
-
- modifiedBy = web.CurrentUser;
-
- if (itemCollection != null && itemCollection.Count > 0) {
- for (int iCount = 0; iCount < itemCollection.Count; iCount++) {
- try {
-
- item = itemCollection[iCount];
- var listItemFormUpdateValueColl = new List < ListItemFormUpdateValue > ();
- ListItemFormUpdateValue listItemFormUpdateValue = new ListItemFormUpdateValue();
- listItemFormUpdateValue.FieldName = "Modified";
- listItemFormUpdateValue.FieldValue = modifiedDate.ToString();
- listItemFormUpdateValueColl.Add(listItemFormUpdateValue);
- item["Editor"] = modifiedBy.Id;
- listItemFormUpdateValue = new ListItemFormUpdateValue();
- listItemFormUpdateValue.FieldName = "Modified_x0020_By";
- listItemFormUpdateValue.FieldValue = modifiedBy.Id.ToString();
- listItemFormUpdateValueColl.Add(listItemFormUpdateValue);
-
- item.ValidateUpdateListItem(listItemFormUpdateValueColl, true, "");
- context.ExecuteQuery();
- } catch (Exception ex) {}
- break;
- }
- }
- }
- } catch (Exception ex) {}
- }
- }
- }
After executing the above code, we can find that the ‘Modified User’ and ‘Modified Date’ field value is updated but the Document/File version remains the same as it was before being updated.
In the below screenshot you can verify the difference.
Before Update
After Update