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

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

Determining Height and Width of Browser’s content window

August 11, 2011 Leave a comment

Hi,
One of the requirement people often have is to show child window which is of size of Browser’s content window Width and Height. Here each browser maintain its own browser width and client. So by default Silverlight provide browser height and width of host by using following code

Application.Current.Host.Content.ActualHeight => Height
Application.Current.Host.Content.ActualWidth => Width

But some times ActualHeight and ActualWidth doesnot give up actual values .
Use the below property to get the height width of host apps.
double height = double.Parse(HTMLPage.Document.Body.GetProperty(“clientHeight”).ToString());

double width=double.Parse(HtmlPage.Document.Body.GetProperty(“clientWidth”).ToString());

-Mahender

“As always please enter your comments”

Categories: Silverlight

HT Session on MEF

July 31, 2011 Leave a comment

Hi,
last week i delivered session on MEF Basic. Please find PPT for the demo

MEF

Please find the Video Session on MEF

-Mahender

Categories: Silverlight Tags:

New features of VS2012 revealed

July 28, 2011 Leave a comment

Recently I was surfing some of important stuff on Silverlight 5 and Windows 8 Operating. I’m surprise to see , Microsoft planing to see new version of Visual Studio, Which is called as Visual Studio -V next.

Please check below URL for new features of VS 2012

New features of VS2012 revealed.

One witty in the above blog is “Google It” , Where developers crawl to find solution for runtime errors. I’m surprise why they didnot mention it as “Bing it” .

@MS: Please change text to “Bing It”, Searching for code error in Bing is faster than google 😉

-Mahender

 

 

Categories: Microsoft .NET Tags:

ALL HT SESSION SLIDES

July 16, 2011 Leave a comment

Hi,
I got good response for sessions on Silverlight. Most people asked me keep all session slide in one common blog entry, so they go through one by one. Below are the session

File Access

Data Templates

Data binding in silverlight

silverlight-control-template

silverlight-command

Routed-events-in-silverlight

RIA services

Printing

Networking and communication

Navigation-application-in-silverlight

Mvvm pattern

Resources and styles

Data binding in silverlight

Key boardevents

introduction-to-silverlight

introduction-to-silverlight-control-3

introduction-to-silverlight-control-4

Html bridge

Dependency property

Data validation in silverlight

Background thread

wcf-ria-services

Happy learning Silverlight

-Mahender

Categories: Silverlight Tags:

HT Session on Backgroundworker Thread

July 16, 2011 Leave a comment

Hello Everyone,

Welcome to another exciting session on Silverlight, It is Background worker thread. Why do we need background-worker thread, can’t we make our code executed in UI Thread?. These are question people comes into mind, people who are pretty new to background thread.

When we develop Windows or WPF apps, If the user performs actions on UI which makes calls to long running services and your UI remain freeze or non responsive, because your UI thread is in waiting state, so to make UI more intutive and responsive, we need to implement background worker thread in your app. You have to move all your services operation calls to Background-worker thread from UI Thread. for rest of content , Please
click on below link for recorded version of Background worker thread session

Presentation : Background Thread
or

Demo Code:BackgroundWorkerThread

Please do enter your comments 🙂

With Regards,
Mahender

%d bloggers like this: