In CSOM, basically, there is no direct property to get the versions of the items in a List. By using "Lists Web Service"(/_vti_bin/Lists.asmx), however, we can get the information of all the versions and properties of each item inside a Custom List or a Document Library.
To get the above-required functionality, first of all, we have added the "Lists Web Service" in the required project.
In this blog, I am going to share all the codes for getting the version details of list items by using "Lists Web Service". We all know how to add a service to a project.
We are using the below code to achieve the same. The code is self-explanatory because of the comments I have written before each snippet.
  1. using System;
  2. using System.Net;
  3. using System.Xml;
  4. using Microsoft.SharePoint.Client;
  5. namespace ConsoleApp1 {
  6. class Program {
  7. static void Main(string[] args) {
  8. ClientContext context = null;
  9. List list = null;
  10. ListItemCollection itemCollection = null;
  11. ListItem item = null;
  12. string userName = string.Empty;
  13. string password = string.Empty;
  14. string dateHistory = string.Empty;
  15. string commentHistory = string.Empty;
  16. string editor = string.Empty;
  17. string loginName = string.Empty;
  18. try {
  19. using(context = new ClientContext("http://SourceWebUrl")) {
  20. userName = "UserName";
  21. password = "PassWord";
  22. // Setting credential for the above site
  23. context.Credentials = new NetworkCredential(userName, password);
  24. context.Load(context.Web);
  25. context.ExecuteQuery();
  26. // Getting list by Title
  27. list = context.Web.Lists.GetByTitle("Custom List");
  28. context.Load(list, L => L.Id);
  29. // Getting all items from selected list using caml query
  30. itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
  31. //Loading selected list items
  32. context.Load(itemCollection, IC => IC.Include(I => I.Id, I => I.DisplayName));
  33. context.ExecuteQuery();
  34. if (itemCollection != null && itemCollection.Count > 0) {
  35. for (int iCount = 0; iCount < itemCollection.Count; iCount++) {
  36. try {
  37. item = itemCollection[iCount];
  38. ListService.Lists listService = new ListService.Lists();
  39. listService.Url = context.Url + "/_vti_bin/Lists.asmx";
  40. listService.Credentials = context.Credentials;
  41. //Getting all item versions from custon list item using List Web Service
  42. XmlNode nodeVersions = listService.GetVersionCollection(list.Id.ToString(), item.Id.ToString(), "_UIVersionString");
  43. //looping all versions and getting 'Modified' and 'Editor' property of each version
  44. foreach(XmlNode xNode in nodeVersions) {
  45. try {
  46. dateHistory = xNode.Attributes["Modified"].Value;
  47. dateHistory = FormatDateFromSP(dateHistory);
  48. commentHistory = xNode.Attributes["_UIVersionString"].Value;
  49. loginName = xNode.Attributes["Editor"].Value;
  50. } catch {}
  51. }
  52. } catch (Exception ex) {}
  53. }
  54. }
  55. }
  56. } catch (Exception ex) {}
  57. }
  58. private static string FormatDateFromSP(string dateHistory) {
  59. string result;
  60. result = dateHistory.Replace("T", " ");
  61. result = result.Replace("Z", "");
  62. return result;
  63. }
  64. }
  65. }
I am using a simple console application to iterate all the item version information.