How to get all SharePoint List item versions using 'Client Side Object' model both for Custom list and Document Library?
Basically, in CSOM, there is no direct property to get the item versions from a List Item. But by using "Lists Web Service"(/_vti_bin/Lists.asmx) we can get all versions and properties information for each item inside Custom List or 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 share all codes how we can get all List Items version by using "Lists Web Service". We all know how to add a service inside a project.
I am using a simple Console application to iterate all item versions information. Please follow the below code...
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";
// Setting credential for the above site context.Credentials = new NetworkCredential(userName, password); context.Load(context.Web); context.ExecuteQuery();
// Getting list by Title list = context.Web.Lists.GetByTitle("Custom List"); 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)); 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;
//Getting all item versions from custon list item using List Web Service XmlNode nodeVersions = listService.GetVersionCollection(list.Id.ToString(), item.Id.ToString(), "_UIVersionString");
//looping all versions and getting 'Modified' and 'Editor' property of each version 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; } } }
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















