Programmatically get ListItemVersion using client object model SharePoint 2010
Asked Answered
E

3

8

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();

    }
}
Elbring answered 14/3, 2013 at 12:0 Comment(0)
C
0

You have to initialize web.ServerRelativeUrl like this

oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.Load(web, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();
Chungchungking answered 12/11, 2013 at 11:24 Comment(2)
No luck, I get The object specified does not belong to a list.Cynthy
If I use var oldFile = web.GetFileByServerRelativeUrl("/" + _version.Url); clientContext.Load(file, item => item.ListItemAllFields); clientContext.ExecuteQuery(); I get a Value does not fall within the expected range. error.Cynthy
A
0

you can get older version file like this

string versionItemUrl = file.ServerRelativeUrl.Replace(Path.GetFileName(file.ServerRelativeUrl),"") + _version.Url;
File oldFile = web.GetFileByServerRelativeUrl(versionItemUrl); clientContext.Load(oldFile, f=>f.ListItemAllFields);
clientContext.ExecuteQuery();
Ardussi answered 2/4, 2016 at 7:20 Comment(0)
S
-1

You should be able to get the list item data using SPFileVersion.Properties which will give you a hashtable of the file's metedata see MSDN - SPFileVersion.Properties Property.

Inside your foreach try

Hashtable oHash = oFileVersion.Properties;
ICollection collKeys = oHash.Keys;

foreach (object oKey in collKeys)
{
    Console.WriteLine(oKey.ToString() + " :: " + oHash[oKey.ToString()].ToString());
}
Shalon answered 1/8, 2013 at 9:30 Comment(1)
Server object model classes do not provide too much help when trying to utilize the client object model.Subtrahend

© 2022 - 2024 — McMap. All rights reserved.