使用客户端对象模型SharePoint 2010以编程方式获取ListItemVersion

时间:2013-03-14 12:00:32

标签: sharepoint sharepoint-2010 .net-3.5 client-object-model sharepoint-clientobject

我有一个场景,我必须将SharePoint 2010列表中的所有数据(name =“VersionTestList”)移动到SQL Server数据库。由于在列表中启用了版本控制,我还想移动以前的版本细节。无论如何,我能够移动最新的项目,但不幸的是我无法获得以前的版本数据。我已经尝试使用客户端对象模型并且能够获取版本,但无法获取相应版本的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();

    }
}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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();

答案 2 :(得分:-1)

您应该能够使用SPFileVersion.Properties获取列表项数据,这将为您提供文件的metedata哈希表,请参阅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());
}