我有一个场景,我必须移动我所有的数据在SharePoint 2010列表(name= "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();
}
}
你必须像这样初始化web.ServerRelativeUrl
oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.Load(web, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();
你可以得到像这样的旧版本文件
string versionItemUrl = file.ServerRelativeUrl.Replace(Path.GetFileName(file.ServerRelativeUrl),"") + _version.Url;
File oldFile = web.GetFileByServerRelativeUrl(versionItemUrl);
clientContext.Load(oldFile, f=>f.ListItemAllFields);
clientContext.ExecuteQuery();
您应该能够使用SPFileVersion.Properties
获得列表项数据,这将为您提供文件的元数据的哈希表,请参阅MSDN - SPFileVersion。属性财产。
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());
}