I recently had a requirement around linking to and downloading specific versions of documents stored in SharePoint. There is some rather quirky behaviour around SPFileVersion and how this is achieved and I felt it warranted a post.
The first thing to be aware of is that there are two distinct version collections:
SPListItemVersionCollection. These collections can be accessed via the respective assets as you would expect (
SPListItem.Versions) and, as is the way in SharePoint, are enumerations of
SPListItemVersion objects, respectively. These objects, which represent the individual versions, have a number of similar properties and methods but do not share a base class nor an interface.
In order to access specific versions of documents, the approach I took was to store the
VersionLabel (a property on both SPFileVersion and SPListItemVersion) and then retrieve the version by using this value in conjunction with
GetVersionFromLabel (a method on both
SPListItemVersion). This works but with a number of caveats:
SPFileVersionCollectiondoes NOT contain an entry for the latest version. This means that if the document has only a single version then this collection will be empty. If you want to access the latest version you must verify that the version label refers to the latest version by comparing it to the first entry in the
SPListItemVersionCollectionand if it is then access the
SPFiledirectly. If you are accessing properties that are also present on an
SPListItemVersion(ie not opening a stream) you can use that collection instead but you must also take note of the next point.
SPListItemVersion.Urlbehaves unexpectedly in some situations. Assumedly this is to support directing underprivileged users away from minor versions that they are not allowed to see (although I don’t see how it achieves this), the
Urlfor minor versions will be equal to that of the latest minor version when there is no published version. Always use
- Minor versions get promoted when published, breaking links to the minor version. Example – You have a document currently at version 0.2, with a previous 0.1 version. When you publish this document you still only have two versions: 0.1 and 1.0. You will now fail to locate version 0.2. Don’t be fooled into thinking that using the
VersionIdproperty will solve this issue. It simply represents the the version label as an integer (+1 for each minor version, +512 for each major version). The only way I could find to handle this case is to introduce the doc store version which is stored in the property bag of the SPFile:
SPFile.Properties["vti_docstoreversion"]. You can see how this works by looking at the code examples I have included but if this specific case isn’t going to be an issue for you then don’t use it and enjoy cleaner code (see here). Also note that the doc store version is only stored in the SPFile property bag and not in the SPListItem property bag.
I have included some code to illustrate how this may be implemented:
Please leave a comment and if you want to keep reading more about SharePoint don’t forget to subscribe.