Explaination on SPFileVersionCollection and SPListItemVersionCollection
This article explains when to use SPFileversionCollection and SPLISTITEMVersionCollection. Most of the developers confuse about the usage of above 2 classes. I hope my article helps other sharepoint developers
This is my first post on share point, This article 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)
{
arrayversionIDs.add(spFileVersion.ID);
}
for(int i=0;i
spFile.Versions.DeleteByID(arrayversionIDs[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.
You can also check my blog for more information on Share point and Silverlight from my Blog
-Mahender