Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Where have my versions gone?

leave a comment »

Once enabled for a library or list, major and minor versions of documents and items can be kept; the number of which can be specified in the library or list settings:

Versions

The version history for items or documents can be viewed, and old versions can be opened and inspected. But where are these old versions stored?

Version histories are maintained for files and for property (column) values. In the case of a document library both a file and a property history is maintained, but for a list item only a property history is kept. The SharePoint class libraries or web services can be used to programmatically access this information, and also determine the URL where a pervious version of a document is located.

So, on a SharePoint server create a new Visual Studio project and add a reference to the “Microsoft.SharePoint.dll” library. Then add a using statement to “Microsoft.SharePoint.” Assuming you have already a reference to a SPList object representing the list or library you can obain a reference to the collection of previous file versions:

            SPFileVersionCollection theFileVersions = lstItem.File.Versions;

The collection can be iterated, and in this case, the version label and URL are added to a WinForm data grid view control:

            foreach (SPFileVersion liVer in theFileVersions)

            {

               int rowID = dgvVersions.Rows.Add();

               dgvVersions.Rows[rowID].Cells[0].Value = liVer.VersionLabel;

               dgvVersions.Rows[rowID].Cells[1].Value = liVer.Url;

            }

The output will look like the following:

Versions2

You can see from this that the previous versions are stored in a virtual folder called “_vti_history” under the site in which the library is located. The virtual folder “512” represents the version number (taking into account other versioned documents in the site). This URL together with the URL for the site can be used to directly open previous versions.

The SPFileVersion object can also be used to access the contents of the file. In this code, the bytes associated with the version are obtained and then saved to a file:

            byte[] outBytes = liVer.OpenBinary();

            using (BinaryWriter binWriter =

               new BinaryWriter(File.Open(@”C:\Temp\MyFile.xlsx”,

                    FileMode.Create)))

            {

               binWriter.Write(outBytes);

            }

This works well with documents, but the situation is much more complex with pages, content and web parts. Navigating to the URL for pages will result in an HTTP 404 – “file not found”.

Advertisements

Written by Nick Grattan

July 31, 2007 at 3:46 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: