none
How to get List Item versions using CSOM RRS feed

  • Question

  • I have a requirement to read the list the list item versions using CSOM. Please suggest.

    Prateek Srivastava

    Friday, September 22, 2017 8:00 AM

All replies

  • Any update

    Prateek Srivastava

    Friday, September 22, 2017 11:15 AM
  • Hi,

    CSOM doesn't provide API to  get version history of a list item.

    As a workaround, we can use SharePoint web service to retrieve the version history and related data of list item In client-side.

    I have written a demo below.

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ConsoleApplicationGetItemVersion
    {
        class Program
        {
            static void Main(string[] args)
            {
                FetchItemOfAppointedVersionByWebService("http://wakaka:17710/sites/hello_world","List1","1","Title");                
            }
    
            public static void getListData(string weburl,string listName)  
            {  
                ConsoleApplicationGetItemVersion.wakaka.Lists myservice = new ConsoleApplicationGetItemVersion.wakaka.Lists();  
                myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;  
                myservice.Url = weburl + "/_vti_bin/Lists.asmx";  
      
                try   
                {  
                    /* Assign values to pass the GetListItems method*/  
                    string viewName = "";  
                    string rowLimit = "100";  
      
                    // Instantiate an XmlDocument object  
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();  
                    System.Xml.XmlElement query = xmlDoc.CreateElement("Query");  
                    System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");  
                    System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");  
      
                    /*Use CAML query*/  
                    query.InnerXml = string.Format("<Where><And>" +  
                        "<Gt>" +  
                        "<FieldRef Name='ID' /><Value Type='Counter'>{0}</Value>" +  
                        "</Gt>" +  
                        "<Leq><FieldRef Name='ID' /><Value Type='Counter'>{1}</Value></Leq>" +  
                        "</And></Where>", 0, 100);  
                    viewFields.InnerXml = "<FieldRef Name='Title' />" +  
                        "<FieldRef Name='Created' />" +  
                        "<FieldRef Name='Modified' />" +  
                        "<FieldRef Name='Author' />" +  
                        "<FieldRef Name='Editor' />";  
                    queryOptions.InnerXml = "";  
      
                    System.Xml.XmlNode nodeListItems = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);  
                    XmlDataDocument xmlDocResult = new XmlDataDocument();  
                    xmlDocResult.LoadXml(nodeListItems.InnerXml);  
                    XmlNodeList rows = xmlDocResult.GetElementsByTagName("z:row");  
                    foreach(XmlNode attribute in rows) {  
      
                        Console.WriteLine(attribute.Attributes["ows_Title"].Value);  
                        string AttachmentUrl = GetAttachmentUrls(weburl, listName, attribute.Attributes["ows_ID"].Value, "");  
                        string vesrsion = GetVersions(weburl, listName, attribute.Attributes["ows_ID"].Value, "Title");  
                    }
                    
                } catch (Exception ex)  
                {  
                    Console.WriteLine(ex.Message);  
                }
                Console.ReadLine();
            }  
      
            public static string GetAttachmentUrls(string siteUrl, string listId, string itemId, string fieldName)  
            {  
                StringBuilder sb = new StringBuilder();  
      
                ConsoleApplicationGetItemVersion.wakaka.Lists listService = new ConsoleApplicationGetItemVersion.wakaka.Lists();  
      
                listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
                listService.Url = siteUrl + "/_vti_bin/lists.asmx"; 
                
                XmlNode nodeAttachments = listService.GetAttachmentCollection(listId, itemId);  
      
                List < string > values = new List < string > ();  
      
                foreach(System.Xml.XmlNode xNode in nodeAttachments)  
                {  
                    values.Add(xNode.InnerText);  
                }  
      
                return string.Join(";", values.ToArray());
    
            }  
            public static string GetVersions(string siteUrl, string listId, string itemId, string fieldName)  
            {
                
                StringBuilder sb = new StringBuilder();  
                ConsoleApplicationGetItemVersion.wakaka.Lists listService = new ConsoleApplicationGetItemVersion.wakaka.Lists();  
                listService.Credentials = System.Net.CredentialCache.DefaultCredentials;  
                listService.Url = siteUrl + "/_vti_bin/lists.asmx";  
     
                if (!string.IsNullOrEmpty(fieldName))   
                {  
                    XmlNode nodeVersions = listService.GetVersionCollection(listId, itemId, fieldName);  
                    foreach(System.Xml.XmlNode xNode in nodeVersions)  
                    {  
                        string dateHistory = xNode.Attributes["Modified"].Value;  
                        dateHistory = FormatDateFromSP(dateHistory);  
                        string commentHistory = xNode.Attributes[fieldName].Value;  
                        string editor = GetEditor(xNode.Attributes["Editor"].Value);  
                        sb.Append(editor + " (" + dateHistory + ") " + commentHistory + "\n\n");  
                    }  
                }  
                return sb.ToString();
              
            }  
      
            private static string FormatDateFromSP(string dateHistory)  
            {  
                    string result;  
      
                    result = dateHistory.Replace("T", " ");  
                    result = result.Replace("Z", "");  
      
                    return result;  
                }  
                /// <summary>  
                /// The XmlNode for version on the Editor contains the Editor Name  
                /// </summary>  
                /// <param name="ienumEditor"></param>  
                /// <returns></returns>  
            private static string GetEditor(string nodeValue)  
            {  
                string[] arr;  
                char[] sep =  
                  {  
                    '#'  
                };  
                // Go for the Editor attribute value  
                // A sample is: 30;#Jo�o Faneca,#VIATECLA\\jfaneca,#joao.faneca@viatecla.pt,#joao.faneca@viatecla.pt,#Jo�o Faneca  
                arr = nodeValue.Split(sep);  
                // Grab the second element for the array  
                nodeValue = arr[1];  
                // Remove the last comma from the Editor value  
                return nodeValue.Remove(nodeValue.Length - 1);  
            }  
          
            /// <summary>
            /// the way of use SharePoint Web Services to fetch Item Version data,
            /// </summary>
            /// <param name="siteUrl"></param>
            /// <param name="listName"></param>
            /// <param name="itemId"></param>
            /// <param name="fieldName"></param>
            public static void FetchItemOfAppointedVersionByWebService(string siteUrl, string listName, string itemId, string fieldName)
            {
                ClientContext ctx = new ClientContext(siteUrl);
                Web web = ctx.Web;
                ctx.Load(web, w => w.ServerRelativeUrl, w => w.Lists);
                List list = web.Lists.GetByTitle(listName);
                ctx.Load(list);
                ctx.ExecuteQuery();
                Console.WriteLine(GetVersions(siteUrl, list.Id.ToString(), itemId, fieldName));
                Console.ReadLine();
            }
        }
    }
    

    Here is my result.

    More reference:

    How to add SharePoint web service

    https://msdn.microsoft.com/en-us/library/ms458094(v=office.14).aspx

    Best regards,

    Lee Liu


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, September 25, 2017 10:26 AM
  • Hi Lee,

    I believe the lists.asmx has been deprecated and Microsoft do not provide any further support on this.

    Is there any other workaround for this?


    Prateek Srivastava

    Wednesday, July 4, 2018 9:28 AM