Archive

Archive for September, 2011

Explaination on SPFileVersionCollection and SPListItemVersionCollection

September 14, 2011 Leave a comment

Hi,
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)
{
arrayversionIDs.add(spFileVersion.ID);
}
for(int i=0;i<arrayversionIDs.Count ;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.

-Mahender

Advertisements

Silverlight 5 RC release

September 3, 2011 Leave a comment

Hi,

Microsoft has launched its Silverlight RC release on September . Please go through Pete Brown blog. It’s for only developer release. RC version includes 40 new features in SL . To know more about the features please go through this link . Some of the high level features like Debugging Binding,Improved media support and rich UI capabilities,implicit data templates etc. Next week onwar, I’m starting tutorials on Silverlight . Stay tune for examples.

-Mahender

Categories: Silverlight
%d bloggers like this: