我有一个场景,我必须将 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();
}
}