How to retrieve document set version history

21 Aug

Document set versions are slightly different than item versions. Document sets can be managed by a separate ribbon tab called Document Set and group called Manage.

To create a version of a document set the action Capture Version in this part of the ribbon has to be selected. When selecting the following screen will be shown:

To use versioning of a document set (and items) versioning has to be enabled on the library.    

After selecting a version option (when major/minor enabled), adding some comment and selecting the Ok button a document set version is created. To view the versions of the document set the action Version History can be selected in the ribbon.

In this screen the first column displayed is No. This is not the regular version column of the library, but a totally different one. When creating a document set version the version column of the library doesn’t change, only the No value.    

That’s nice, but where is the version of the document set actually stored?    

The version(s) of a document set are stored in the propertybag of the item itself.    

To analyze settings I always start up PowerShell first to check on things. Just because it’s quick and easy. When I get what I want from PowerShell it’s easily turned into C# code. I followed the same procedure to get to the storage of document set versions. Since I couldn’t find the version anywhere in the UI, my first guess was checking the propertybag keys.    

$site=Get-SPSite "http://sp2010dev"
$docList = $site.RootWeb.Lists.TryGetList("Documents");
$item = $docList.Items.GetItemById(2)
$prop = $item.Properties

Often I use PowerGui Script Editor which gives an excellent overview of variables:    

The keys:

And the values:

While scrolling through the property keys I noticed a key named snapshots. By checking out the value of that key I knew I found it!    

The following PowerShell command can be used to get the value of the snapshot key:    

$item.Properties.get_Item("snapshots")

The version history is stored in xml:    

<SnapshotCollection NextSnapshotNumber="3" NextInternalId="1">
  <Items />
  <Snapshots>
    <Snapshot Label="2" Major="True" Created="08/21/2011 07:50:56" By="username">
      <Comments>Another version of the document set.</Comments>
      <Fields>
        <Field Id="8553196d-ec8d-4564-9861-3dbe931050c8">Document set name</Field>
        <Field Id="fa564e0f-0c70-4ab9-b863-0177e6ddd247">Document set name</Field>
        <Field Id="cbb92da4-fd46-4c7d-af6c-3128c2a5576e">Add a description here.</Field>
      </Fields>
      <SnapshotItems />
    </Snapshot>
    <Snapshot Label="1" Major="True" Created="08/21/2011 07:50:20" By="username">
      <Comments>This is the first version of this document set.</Comments>
      <Fields>
        <Field Id="8553196d-ec8d-4564-9861-3dbe931050c8">Document set name</Field>
        <Field Id="fa564e0f-0c70-4ab9-b863-0177e6ddd247">Document set name</Field>
        <Field Id="cbb92da4-fd46-4c7d-af6c-3128c2a5576e" />
      </Fields>
      <SnapshotItems />
    </Snapshot>
  </Snapshots>
</SnapshotCollection>

   

The Snapshot element with Label attribute 1 is the first version. The comment is displayed and the fields and values of the document set.    

The Snapshot element with Label attribute 2 is the second version. The same items are displayed with the addition of the value of the description ‘Add a description here’. That’s what I changed before creating the second version.    

The attribute NextSnapshotNumber holds the value of the version number that will be created when creating another version, 3 in this case.    

Summary  

Document set versions are different than regular item versions. The version history of a document set is stored in the propertybag of the document set itself as xml.