I have a scenario where I have to move all my data in a SharePoint 2010 list (name= "VersionTestList") to a SQL server database. Since versioning is enabled in the list, I want to move the previous version details too. Anyway I was able to move the latest item, but unfortunately I am not able to get the previous version data. I have tried this using Client Object Model and able to get the versions, but not able to get the ListItem of that corresponding version. Please find below the code which I have tried till now and give me a hand in resolving this.
Also, I am taking the version of the ListItem like this:
string path = web.ServerRelativeUrl + "/Lists/VersionTestTable/1_.000";
File file = web.GetFileByServerRelativeUrl(path);
clientContext.Load(file, item=>item.ListItemAllFields);
FileVersionCollection versions = file.Versions;
clientContext.Load(versions);
oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.ExecuteQuery();
My entire code is like this:
class Program
{
static void Main(string[] args)
{
GetVersionsUsingCOM();
}
public static void GetVersionsUsingCOM()
{
File file;
FileVersionCollection versions;
IEnumerable<Microsoft.SharePoint.Client.FileVersion> oldVersions;
ClientContext clientContext = new ClientContex("http://server:1200/test/Poc");
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
string path = web.ServerRelativeUrl + "/Lists/VersionTestTable/1_.000";
file = web.GetFileByServerRelativeUrl(path);
clientContext.Load(file, item=>item.ListItemAllFields);
//clientContext.ExecuteQuery();
versions = file.Versions;
clientContext.Load(versions);
oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.ExecuteQuery();
if (oldVersions != null)
{
foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
{
int count=0;
Console.WriteLine(_version.CheckInComment);
Console.WriteLine("Version : {0}", _version.VersionLabel);
//// Working fine till here but unable to get the version details from version.Url
string versionItemUrl = web.ServerRelativeUrl +"/" + _version.Url;
File oldFile = web.GetFileByServerRelativeUrl(versionItemUrl);
clientContext.Load(oldFile, f=>f.ListItemAllFields);
clientContext.ExecuteQuery();
Console.WriteLine(oldFile.ListItemAllFields["Name"]);
count++;
}
oldVersions = null;
}
Console.ReadLine();
}
}