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