Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPFileVersionCollection - why versions are sorted in mixed order?

SPFileVersionCollection and SPListItemVersionCollection versioning seems inconsistent to me. Inconsistency wouldn't be a problem to me, but sort order is.

SPListItemVersionCollection

I can understand versioning of ListItems as they are stored in descending order:

SPContext.Current.ListItem.Versions.Count -> 5
SPContext.Current.ListItem.Versions[0].VersionId -> 1026 (2.2 latest version)
SPContext.Current.ListItem.Versions[1].VersionId -> 1025 (2.1)
SPContext.Current.ListItem.Versions[2].VersionId -> 1024 (2.0)
...                                [4].VersionId ->      (oldest version)

SPFileVersionCollection

However I can't understand how version numbers are saved for a document library item:

SPContext.Current.ListItem.File.Versions.Count -> 4
SPContext.Current.ListItem.File.Versions[0].ID -> 512 (1.0 oldest one) 
SPContext.Current.ListItem.File.Versions[1].ID -> 513 (1.1)
SPContext.Current.ListItem.File.Versions[2].ID -> 1025 (2.1 latest version)
SPContext.Current.ListItem.File.Versions[3].ID -> 1024 (2.0 (EDIT: IsCurrentVersion = True))

They are nor in ascending order, nor descending, but something mixed.

Is there any reason for SharePoint team to decide to store SPFile versions like that? And do they expect that I write my own method to get latest version or is there a builtin one for that?

A note: Let me point out that SPListItem.File is not null for document library items.

like image 427
Janis Veinbergs Avatar asked Feb 26 '10 10:02

Janis Veinbergs


1 Answers

Thought I would output some info about SPFileVersionCollection in console app and it turns out that:

  • last index will hold the current (not drafted but published) version (SPFileVersion.IsCurrentVersion property is TRUE for this)
  • 0 index holds the oldest version
  • (last index - 1) has the last drafted version (for example 2.7) and (last index - 2) holds 2.6 etc.
  • SPFile.Versions.Count = 0 if you just uploaded brand new document (minor version, before publishing).
  • If you continue uploading new document versions and have not yet published one, then they add to SPFileVersionCollection, however none has IsCurrentVersion property set to true until you publish one.

Ahh, i had a false assumption that a last document draft version should count as the latest one. Ofcourse, the last PUBLISHED version is the latest one!

Still, beware of inconsistency.

like image 149
Janis Veinbergs Avatar answered Sep 27 '22 22:09

Janis Veinbergs