Programmatically get ListItemVersion using client object model SharePoint 2010

巧了我就是萌 提交于 2020-01-01 05:01:46

问题


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

    }
}

回答1:


You have to initialize web.ServerRelativeUrl like this

oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.Load(web, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();



回答2:


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:


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


来源:https://stackoverflow.com/questions/15408687/programmatically-get-listitemversion-using-client-object-model-sharepoint-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!