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.
- using System;
- using System.Net;
- using System.Xml;
- using Microsoft.SharePoint.Client;
- namespace ConsoleApp1 {
- class Program {
- static void Main(string[] args) {
- ClientContext context = null;
- List list = null;
- ListItemCollection itemCollection = null;
- ListItem item = null;
- string userName = string.Empty;
- string password = string.Empty;
- string dateHistory = string.Empty;
- string commentHistory = string.Empty;
- string editor = string.Empty;
- string loginName = string.Empty;
- try {
- using(context = new ClientContext("http://SourceWebUrl")) {
- userName = "UserName";
- password = "PassWord";
-
- context.Credentials = new NetworkCredential(userName, password);
- context.Load(context.Web);
- context.ExecuteQuery();
-
- list = context.Web.Lists.GetByTitle("Custom List");
- context.Load(list, L => L.Id);
-
- itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
-
- context.Load(itemCollection, IC => IC.Include(I => I.Id, I => I.DisplayName));
- context.ExecuteQuery();
- if (itemCollection != null && itemCollection.Count > 0) {
- for (int iCount = 0; iCount < itemCollection.Count; iCount++) {
- try {
- item = itemCollection[iCount];
- ListService.Lists listService = new ListService.Lists();
- listService.Url = context.Url + "/_vti_bin/Lists.asmx";
- listService.Credentials = context.Credentials;
-
- XmlNode nodeVersions = listService.GetVersionCollection(list.Id.ToString(), item.Id.ToString(), "_UIVersionString");
-
- foreach(XmlNode xNode in nodeVersions) {
- try {
- dateHistory = xNode.Attributes["Modified"].Value;
- dateHistory = FormatDateFromSP(dateHistory);
- commentHistory = xNode.Attributes["_UIVersionString"].Value;
- loginName = xNode.Attributes["Editor"].Value;
- } catch {}
- }
- } catch (Exception ex) {}
- }
- }
- }
- } catch (Exception ex) {}
- }
- private static string FormatDateFromSP(string dateHistory) {
- string result;
- result = dateHistory.Replace("T", " ");
- result = result.Replace("Z", "");
- return result;
- }
- }
- }
I am using a simple console application to iterate all the item version information.