Monday, 14 June 2010


Recently I had a task to run an audit of a document library and to retrieve files and versions where specific fileds had changed for a version or a new version of the file had been uploaded.

Comparing changes in metadata fields across versions is easy, you simply get the versions collection of a listitem using: SPLIstItem.Versions[int] and then use the field properties to get the value for a field for a specific version e.g. SPLIstItem.Versions[int]["Revision"].ToString().

I tried to do the same thing for file sizes : SPLIstItem.Versions[int]["FileSize"]. One would think that this would retrieve the size of the file for this specific version. The answer is no. This code retrieves the latest size of the file, meaning no matter which version is specified, it will always return the same value.

The way to determine the file size for a specific version is to use the versions collection of the SPListItem i.e. SPistItem.File.Versions. When iterating through the SPFile.Versions colelction, there is an important point to note.

The interesting point is in comparing the version count for the 2 methods of retrieving versions:

SPListItem.Versions.Count vs SPListItem.SPFile.Versions.Count

SPListItem.Versions.Count is always 1 more than SPListItem.SPFile.Versions.Count. The reason for this is because SPFile deems the latest version as the current version and therefore does not include this in its version collection. Therefore SPListItem.Versions.Coun[1] represents the same entity as SPListItem.SPFile.Versions.Count[0].

An example of how I have used this is :

int versionCount = 2;
//current_Item is an SPListItem object;
string status = current_Item.Versions[versionCount]["Status"].ToString();
int currentSize = current_Item.File.Versions[versionCount - 1].Size;

I hope this saves someone some time!

No comments:

Post a Comment