8

我有一个场景,我必须将 SharePoint 2010 列表(名称=“VersionTestList”)中的所有数据移动到 SQL 服务器数据库。由于列表中启用了版本控制,我也想移动以前的版本详细信息。无论如何,我能够移动最新的项目,但不幸的是我无法获得以前的版本数据。我已经尝试过使用客户端对象模型并能够获取版本,但无法获取相应版本的 ListItem。请在下面找到我到目前为止尝试过的代码,并帮助我解决这个问题。

另外,我正在使用这样的 ListItem 版本:

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

我的整个代码是这样的:

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

    }
}
4

3 回答 3

0

你必须web.ServerRelativeUrl像这样初始化

oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.Load(web, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();
于 2013-11-12T11:24:25.323 回答
0

你可以像这样得到旧版本的文件

string versionItemUrl = file.ServerRelativeUrl.Replace(Path.GetFileName(file.ServerRelativeUrl),"") + _version.Url;
File oldFile = web.GetFileByServerRelativeUrl(versionItemUrl); clientContext.Load(oldFile, f=>f.ListItemAllFields);
clientContext.ExecuteQuery();
于 2016-04-02T07:20:20.930 回答
-1

您应该能够获取列表项数据,使用SPFileVersion.Properties它将为您提供文件元数据的哈希表,请参阅MSDN - SPFileVersion.Properties Property

在你的foreach尝试里面

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

foreach (object oKey in collKeys)
{
    Console.WriteLine(oKey.ToString() + " :: " + oHash[oKey.ToString()].ToString());
}
于 2013-08-01T09:30:51.153 回答