Programmatically get ListItemVersion using client

2019-03-27 04:54发布

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

    }
}

3条回答
唯我独甜
2楼-- · 2019-03-27 05:16

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();
查看更多
看我几分像从前
3楼-- · 2019-03-27 05:17

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());
}
查看更多
beautiful°
4楼-- · 2019-03-27 05:27

You have to initialize web.ServerRelativeUrl like this

oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.Load(web, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();
查看更多
登录 后发表回答