Update a SharePoint ListItem without increasing its item file version using SharePoint Client Side Model(CSOM).

Basically, when we are trying to update a SharePoint ListItem/Document then the item/file version is increased according to the list versioning. In the Server side object model, we can update a ListItem 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 a 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
            {           
                // Site Url to scan
                webUrl = "https://SiteUrl";

                using (context = new ClientContext(webUrl))
                {
                    userName = "UserName";
                    password = "Password";

                    foreach (char c in password.ToCharArray())
                        pswd.AppendChar(c);

                    // Setting credential for the above site                    
                    context.Credentials = new SharePointOnlineCredentials(userName, pswd);

                    web = context.Web;
                    context.Load(web, w => w.ServerRelativeUrl, w => w.Id, w => w.CurrentUser);
                    context.ExecuteQuery();

                    // Getting list by Title
                    list = context.Web.Lists.GetByTitle("Documents");
                    context.Load(list, L => L.Id);

                    // Getting all items from selected list using caml query
                    itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());

                    //Loading selected list items
                    context.Load(itemCollection, IC => IC.Include(I => I.Id, I => I.DisplayName, I => I.File, I => I.File.ServerRelativeUrl));
                    context.ExecuteQuery();

                    // The modified date will be changed to below date after the excuted successfully
                    modifiedDate = "20/01/2019";

                    // The modified user will be changed to current user after the code excuted successfully
                    modifiedBy = web.CurrentUser;

                    // Looping each item
                    if (itemCollection != null && itemCollection.Count > 0)
                    {
                        for (int iCount = 0; iCount < itemCollection.Count; iCount++)
                        {
                            try
                            {
                                //getting individual list item from item collection
                                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);

                                // This method will update the ListItem properties without increasing file version
                                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 Updated

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Softree Consulting is an award-winning SharePoint consultancy that has been working with clients from across the globe to transform workplaces with tools and platforms from Microsoft technology. As a widely recognized organization, we are commonly approached to incorporate technical excellence and innovative design into all our customer actions.....

One comment: On Update a SharePoint ListItem without increasing its item file version using SharePoint Client Side Model(CSOM).

Comments are closed.

Site Footer