Retrieve the Changes Occurred on the List Item using CSOM

  1. //CSOM Assembly version - 16.1.4727.1200  
  2. //using Microsoft.SharePoint.Client;  
  3. //using System.Net  
  4. //GetItemChanges(credentials, weburl, listtitle, itemid);  
  5.   
  6. //This method returns all the changes (add, update & delete) happened to the list item  
  7. private static void GetItemChanges(ICredentials credentials, string weburl, string listTitle, int itemid)  
  8. {  
  9.     using (ClientContext ctx = new ClientContext(weburl))  
  10.     {  
  11.         ctx.Credentials = credentials;  
  12.         Web oweb = ctx.Web;  
  13.         ListCollection lists = oweb.Lists;  
  14.         List targetList = lists.GetByTitle(listTitle);  
  15.         ListItem targetItem = targetList.GetItemById(itemid);  
  16.         //ChangeQuery helps the method gets the changes by setting appropriate property as true  
  17.         ChangeQuery cq = new ChangeQuery();  
  18.         cq.Item = true;  
  19.         cq.Add = true;  
  20.         cq.Update = true;  
  21.         cq.DeleteObject = true;  
  22.   
  23.         //SP.ListItem.GetChanges(ChangeQuery)  - Method available from CSOM Assembly version - 16.1.4727.1200       
  24.         ChangeCollection cc = targetItem.GetChanges(cq);  
  25.         ctx.Load(cc);  
  26.         ctx.ExecuteQuery();  
  27.         Console.WriteLine(cc.Count.ToString());  
  28.     }  
  29.     Console.WriteLine("Press any key to exit...");  
  30.     Console.Read();  
  31. }