Home > Sharepoint > Explaination on SPFileVersionCollection and SPListItemVersionCollection

Explaination on SPFileVersionCollection and SPListItemVersionCollection

Welcome to my another exciting post on Share-point. This is my first post on share point, This blog explain my findings on SpFileVersionCollection and SPListItemVersionCollection

When we are developing share-point application, there is necessity of obtaining Versions for a File uploaded in Document Library. First of all Why do we need to versions ?. SharePoint is a technology which provides collaboration and also make user to check in and check out the document. Since there is provision of check in and check out , version capability for the document comes into mainline. If there is a document with 10 versions, there is necessity of retrieve all the version’s document-content because either we want to compare between versions or Moving the file from one site to another site etc. So now the question we keep pop up in our mind is how do we get the versions for the documents.

If you get back to Share-point SPObjectModel, there are 2 ways of getting versions for a document, either by using SpFileVersionCollection or SplistItemVersionCollection.

Now lets look into how to access versions using SpFileVersionCollection

foreach (SPListItem doc in DocumentLibraryName.Items)
SPFileVersionCollection coll = doc.File.Versions;
if (coll.Count != 0)
foreach (SPFileVersion version in coll)
Console.WriteLine(‘VersionLabel: ‘ + version.VersionLabel + ‘ IsCurrentVersion: ‘ + version.IsCurrentVersion );

See the Out put

VersionLabel : 1.0 IsCurrentVersion :False

VersionLabel :2.0 IsCurrentVersion :False

VersionLabel : 3.0 IsCurrentVersion :False

VersionLabel : 4.0 IsCurrentVersion :True

Problem with SpFileversionCollection is it doesnot give all version associated with a document i.e It doesnot includes Current version which is in draft Mode. Then How to get all the versions?

Answer is We will make a use of SpListItemVersionCollection

Here is the Code

foreach (SPListItem doc in documentLibrary.Items)
SPListItemVersionCollection coll = doc.Versions;
foreach (SPListItemVersion version in coll)
Console.Writeline(‘VersionLabel: ‘ + version.VersionLabel + ‘ IsCurrentVersion: ‘ + version.IsCurrentVersion )

Now the Output Is

Version Label : 4.1 IsCurrentVersion : True

Version Label : 4 IsCurrentVersion : True

Version Label : 3 IsCurrentVersion : False

Version Label : 2 IsCurrentVersion : False

Version Label :1 IsCurrentVersion : False

In the above way you can get all version of a document in sharepoint object Model. If you wanna programatically remove all the version , then better make use of SpFileVersionCollection, Since it is going to remove/delete all version except the Current version . here are the below code to delete

Foreach (SPListItem doc in documentLibrary.Items)
SPFileVersionCollection coll = doc.File.Versions;

foreach (SPFileVersion version in coll)

//Either use Recycle() or Delete()
item.Recycle(); //or item.Delete()

But the best way for delete versions are usage of versionCollection i.e check below code

Store all versionID in an Array and iterate through each array Item,Like below
foreach(SpFileversion in Coll)
for(int i=0;i<arrayversionIDs.Count ;i++)

IN Order to Identify the publish version then make use of property Level in SPFileVersion to check whether it is Published or Draft or Checkout status.

item.Level == SPFileLevel.Published;

Hope It makes clear in usage of SpFileVersionCollection and SPListItemVersionCollection. Please do comment for my explanation.


  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: