SPFileVersionCollection - why versions are sorted in mixed order?
Asked Answered
A

1

9

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.

Angellaangelle answered 26/2, 2010 at 10:16 Comment(0)
A
6

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.

Angellaangelle answered 26/2, 2010 at 11:1 Comment(2)
your last two dot points seem contradictory to me - if Count is 0 until one is published, how can they be added to the SPFileVersionCollection?Rickeyricki
@Rickeyricki thanks for the comment. Long time since this has been tested, too lazy to go on SharePoint to make sure, but this answer could shed some light: "SPListItem.Versions.Count is always 1 more than SPFile.Versions.Count. The reason is SPFile consider the latest version as the current version and therefore does not include this in its version collection"Angellaangelle

© 2022 - 2024 — McMap. All rights reserved.