English 中文(简体)
我如何通过程序从SharePoint网站检索主页面的先前版本?
原标题:
  • 时间:2009-02-19 04:42:05
  •  标签:

在SharePoint网站中,当您签出/修改/签入主页面时,SharePoint会保留以前的主页面版本,并且您可以在主页面库中看到以前版本的列表。对我来说,查看以前版本的内容的唯一方法似乎是恢复该版本 - 但这将创建一个版本历史记录中的附加条目,这并不是一个新版本,而仅仅是查看以前版本的工件。

最终我找到了一种比较暴力的方法,通过使用stsadm实用程序查看以前的版本:

stsadm -o export -url http://site -fileneme export.cab -versions 4

然后在cab中查找manifest.xml文件以找到指向cab中所需.dat文件的指针,以查看所需的先前版本。现在,对于大型站点,此解决方案显然存在问题,因为导出操作导出整个站点内容。

所以这是我的问题......如果stsadm -o export可以提取文件的旧版本,我猜想有一种通过SharePoint API来编程实现的方法。有人知道是否可能以及如何实现吗?

最佳回答

The SPFileVersion class was indeed the way forward. Here s a snippet of code that will export previous versions of the default.master page out to the file system. One thing to note though is that exporting versions[0] doesn t work - it produces an exception when trying calling ver.OpenBinary. I suspect that this has something to do with the whole ghosted/unghosted issue in SharePoint, that the original version of the file is stored differently to subsequent versions. Running this code for other files that were added to the master page gallery, works when retrieving versions[0]. This seems to be an issue only for files that were in the original uncustomised SharePoint site.

    SPFile file;
    SPFileVersionCollection versions;
    SPFileVersion ver;
    byte[] content;
    FileStream fs;
    SPSite site = new SPSite("http://localhost:6000");


    file = site.RootWeb.GetFile("_catalogs/masterpage/default.master");
    Console.WriteLine(file.Url + ", " + file.Length);
    versions = file.Versions;
    Console.WriteLine(versions.Count);

    for (int i = 1; i < versions.Count; i++)
    {
        ver = versions[i];
        Console.WriteLine(ver.VersionLabel + ", " + ver.Size);
        content = ver.OpenBinary();
        fs = new FileStream("c:\temp\" + ver.VersionLabel + "-default.master",FileMode.Create);
        fs.Write(content, 0, content.Length);
        fs.Close();
    }
问题回答

也许你需要使用SPFileVersion类,通过SPFile对象的versions属性获取。

也请参阅SPListItemVersion类。

你应该能够像获取其他任何列表一样获取主页面库。

SPList masterPageGal = myWeb.Site.RootWeb.Lists["Master Page Gallery"];

如果有人感兴趣,这里是上面代码的PowerShell版本:

function ExportVersions([string] $webUrl, [string]$spfilepath, [string]$filePath)
{
  $web = $null
  try
  {
    $web = get-spweb $webUrl
    $file = $web.GetFile($spfilepath)
    Write-host "Url: " $file.Url 
    Write-host "length: " $file.Length
    Write-host "Number of versions: " $file.Versions.Count
    Write-host
    foreach ($version in $file.Versions)
    {
        Write-Host "Version: " $version.VersionLabel
        Write-Host "Size: " $version.Size
        $content = $version.OpenBinary()
        $spFileName = [System.IO.Path]::GetFileName($spfilepath)
        $outFileName = $filePath + $version.VersionLabel + "-$spFileName"
        $fs = new-object System.IO.FileStream($outFileName,[System.IO.FileMode]::Create)
        $fs.Write($content,0,$content.Length)
        $fs.Close()
    }

    $out = "Finished exporting versions for: " + $spfilepath
    Write-Host $out
    Write-Host

  }
  catch
  {
    throw $_
  }
  finally
  {
    # Clean up
    $web.Close()
  }
}




相关问题
热门标签