A while ago I wrote a blog post on how to retrieve or extract document library version history in SharePoint 2010. Since most of the developers do not have access to SharePoint servers, they can't run PowerShell scripts on SP servers. So I thought of implementing a SharePoint Client Side Object Model (CSOM) solution to retrieve document library version history.

In this blog I will develop a simple Visual C# Console Application to retrieve the document library version history in SharePoint 2010 using Client Side Object Model (CSOM). You can run this application from your machine and there is no need to logon to SharePoint Server.

Before we get started, make sure you've installed SharePoint Foundation 2010 Client Object Model on your machine.

Open Visual studio 2012/2010 and navigate to File > New > Project > Visual C# > Console Application as shown below.

Then add the following references to Visual Studio solution

  1. Microsoft.SharePoint.Client
  2. Microsoft.SharePoint.Client.Runtime

Above DLL's should be located at "C:\Program Files\Common Files\Microsoft Shared\SharePoint Client" once you install SharePoint Foundation 2010 Client Object Model.

The following application will retrieve the version history of a document library by going through each version of the file/document!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;

namespace GetSPDocLibVersionHistory
{
class Program
{
static void Main(string[] args)
{
// Change your site url below
using (ClientContext clientContext = new ClientContext("http://sp2010/CapTechBlog/"))
{
Web site = clientContext.Web;
// Change your document library name
List docLib = site.Lists.GetByTitle("Shared Documents");
clientContext.Load(docLib);

CamlQuery caml = new CamlQuery();
ListItemCollection items = docLib.GetItems(caml);

clientContext.Load(items);
clientContext.ExecuteQuery();

// Iterate through all items in the document library
foreach (ListItem item in items)
{

Console.WriteLine("File Name:: " + item.FieldValues["Title"]);

clientContext.Load(item.File);
FileVersionCollection versions = item.File.Versions;

clientContext.Load(versions);
clientContext.ExecuteQuery();

// If the file has versions, loop through all of the versions
if (versions != null)
{
foreach (FileVersion version in versions)
{
User usr = version.CreatedBy as User;
clientContext.Load(usr);
clientContext.ExecuteQuery();

Console.WriteLine("Version Info:: {0}, {1}, {2}, {3}", version.VersionLabel, version.Created, usr.LoginName, version.CheckInComment);
}
}

FieldUserValue modBy = item["Editor"] as FieldUserValue;
// Get the latest verion of the file
Console.WriteLine("Version Info:: {0}, {1}, {2}, {3}", item.File.UIVersionLabel, item.File.TimeLastModified, modBy.LookupValue, item.File.CheckInComment);
Console.WriteLine("---------------------------------------------------------------");
}

Console.ReadLine();

}
}
}
}

I hope it helps! Let me know if you've any questions.