7

我有一個場景,我必須將SharePoint 2010列表(name =「VersionTestList」)中的所有數據移動到SQL Server數據庫。由於列表中啓用了版本控制,因此我也想移動以前的版本詳細信息。無論如何,我能夠移動最新的項目,但不幸的是我無法獲得以前的版本數據。我已經嘗試過使用客戶端對象模型並能夠獲取版本,但無法獲取相應版本的ListItem。請在下面找到我迄今爲止試過的代碼,並幫我解決這個問題。以編程方式使用客戶端對象模型獲取ListItemVersion SharePoint 2010

而且,這是我在列表項的版本是這樣的:

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

    } 
} 

回答

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

Server對象模型類不提供嘗試利用客戶端對象模型時有太多幫助。 – 2013-11-11 13:33:12

0

您必須初始化web.ServerRelativeUrl這樣

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

不幸的是,我得到了'指定的對象不屬於一個列表。'# – PeterX 2015-06-11 04:42:05

+0

如果我使用'var oldFile = web.GetFileByServerRelativeUrl(「/」+ _version.Url); clientContext.Load(file,item => item.ListItemAllFields); clientContext.ExecuteQuery();'我得到'值不在預期的範圍內.'錯誤。 – PeterX 2015-06-11 04:51:28

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();
相關問題