Documents and versioning

A nice part of document libraries and lists is the version history. Nothing new, we had it in MOSS 2007, but I previously never dived really into the version history by code.
Recently I did and discovered something that might help you out some time. 

Let’s get the files of the Shared Documents folder and get all the versions by code:

SPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists['Shared Documents'] as SPDocumentLibrary;
resultBox.Text = string.Empty;
foreach (SPListItem doc in sharedDocs.Items)
{
      SPFileVersionCollection coll = doc.File.Versions;
      if (coll.Count != 0)
     {
          resultBox.Text += 'Versions of ' + doc.File.Name + Environment.NewLine;
          foreach (SPFileVersion version in coll)
          {
                resultBox.Text += 'VersionLabel: ' + version.VersionLabel + ' IsCurrentVersion: ' + version.IsCurrentVersion + Environment.NewLine;
          }
     }
}

 Nothing quite impressive, I used the file version collection and asked some of the properties from the SPFileVersion. Here’s the result:

Notice that version 5.0 is the currently published version and a draft version 5.1 is available too.
Now let’s take a look at the version history of the file at the document library itself:

 

Hey, there is another version: 5.2! Why does this file not show up by code? 

There is another approach to get all the versions, let’s use the versions of the list items instead of the files: 

SPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists['Shared Documents'] as SPDocumentLibrary;
resultBox.Text = string.Empty;
foreach (SPListItem doc in sharedDocs.Items)
{
     SPListItemVersionCollection coll = doc.Versions;
     resultBox.Text += 'Versions of  ' + doc.Name + Environment.NewLine;
     foreach (SPListItemVersion version in coll)
     {
          resultBox.Text += 'VersionLabel: ' + version.VersionLabel + ' IsCurrentVersion: ' + version.IsCurrentVersion + Environment.NewLine;
     }
}

And the result:
 

Notice that all the existing versions, including the current version, are displayed. Also the sorting is the other way around compared to the listing based on the file versions. 

The conclusion here is that the SPFileVersionCollection only shows the versions, without the current version and the SPListItemVersionCollection shows all the versions, including the current version

Suppose you want to delete or put all the previous versions of a file to the recycle bin, you can easily use the SPFileVersionCollection option demonstrated above. This way you don’t have to exclude the current version from your deleting code as you would with the SPListItemVersionCollection option. 

Here’s an example of this:

SPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists['Shared Documents'] as SPDocumentLibrary;
resultBox.Text = 'To recycle bin: ' + Environment.NewLine;
foreach (SPListItem doc in sharedDocs.Items)
{
     SPFileVersionCollection coll = doc.File.Versions;
     if (coll.Count != 0)
     {
          resultBox.Text += doc.Name + Environment.NewLine;
          doc.File.Versions.RecycleAll();
     }
}

The result of questioning the SPFileVersionCollection after this action:

The result of questioning the SPListItemVersionCollection after this action:

Notice that the current version of the file still exists after the recycle action. With the SPFileVersionCollection there is one version besides the actual current version, which is not listed here. This is because the actual current version is still a draft version.
With the SPListItemVersionCollection both versions are listed, the top one is the draft, the second the published version.

Post to Twitter

34 Responses to “Documents and versioning”

  • medical assistant:

    Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  • Linda Popejoy:

    I had errors viewing the website in Firefox on the Mac, but apart from that loved the site! :)

  • Ira Washam:

    Just take it and thanks the post again.

  • Reinaldo Winokur:

    One should forgive one’s enemies, but not before they are hanged.–Heinrich Heine (1797–1856), German poet

  • Gale Howe:

    Great blog post, you deserve a free iPad

  • Ebbing:

    someone sent me a link here on facebook. I’m happy I decided to check it out, great blog

  • Aquilera:

    I feel you are too good to write Genius!Thanks for posting, maybe we can see more on this.

  • weinland:

    Surprise! You truly covered this subject well. Are there other alternatives that i am going to need to verify out?

  • ask:

    This is one of the most incredible blogs Ive read in a very long time. The amount of information in here is stunning, like you practically wrote the book on the subject

  • Link Building:

    Hello admin, I have a small request. I was just searching for some information on the topic you wrote and found this blog. Some really awesome stuff you shared here, can I please link to this post on my new website I’m workin’ on? It would be great :) . I will check back again later to see how you responded. cheers, John Allen .

  • Anita:

    Ofcourse you can link to this post, no problem!

  • Catherine:

    Thanks much for this process really good publish; this is the type of advantage that maintains me though out the day.We’ve always heard really been looking around in your internet site immediately after I seen about them from a companion and was happy when I was able to locate it right after seeking for some time. Being a experienced blogger, I’m pleased to view other people taking gumption and making contributions on the neighborhood. I just wished to comment to show my appreciation for your personal submit as it’s highly reassuring, and lots of internet writers usually do not get the credit score they deserve. I am positive I’ll be again and will send out a few of my pals.

  • Bradley Ostrov:

    What a write!! Very informative and easy to understand. Looking for more such posts!! Do you have a facebook?
    I recommended it on stumbleupon. The only thing that it’s missing is a bit of new design. However thank you for this information.

  • Angeld:

    Very Interesting Blog! Thank You For Thi Blog!

  • Croatia:

    Thanks for this great post. The info I have gained from your blog is truly encouraging

  • freudenberg:

    Great writing! I wish you could follow up on this topic :P

  • mhi:

    This is the GREATEST paper that I read in my life.

    Kind regards
    Luciano

  • sklep z aplikacjami na telefon:

    Great blog, I have just bookmarked it :)

  • Marylyn Sarcia:

    This is weird, but I can only see part the post, is this the internet website or my browser? I think I should restart.

  • Skarupa:

    Thank you so much for discuss rather beneficial informations. Your web is very good, I am amazed by the information and facts that you have on this blog. It shows how effectively you fully understand this subject. Bookmarked this web page, will appear back for way more. You, my close friend, I observed just the information I currently explored all over the place and just couldn’t acquire. What an ideal site. Similar to this web site your web-site is one of my new most favorite.I similar to this data proven and it has offered me some type of drive to possess success for some purpose, so continue to keep up the great work!

  • Juli Tausch:

    I can’t disagree any of your statements. You should write more!

  • Colin Acero:

    Great job with this article. Trust you’d post more often though. Plently of people can gain from it.

  • Sylvester Kost:

    I’d come to be of the same mind with you on this. Which is not something I usually do! I love reading a post that will make people think. Also, thanks for allowing me to speak my mind!

  • baggy broek:

    Hey bedankt voor de informatie. Ik zal het eens proberen heh.

  • maria andros:

    Hey there this is a fantastic post. I’m going to e-mail this to my pals. I came on this while exploring on aol I’ll be sure to come back. thanks for sharing.

  • James D Despain:

    Thanks for the great post. Bookmarked

  • James P Despain:

    Thanks for the great post.

  • tatil enes:

    It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing…

  • Novitsky:

    You rule for posting this. I needed this.

  • Alphonso Hughen:

    Interesting the way in which really feel regarding Documents and versioning. brbr The thing i would mention is that if you’ll get it done like that then you will want prepared to handle the work. brbr I know that this will not be common because people are extremely nonproductive today.

  • Hobert Sunkel:

    Hello. I actually wanted to firmly leave a nice quick comment and also let you learn that in fact I’ve been focusing on your particular page for quite some time. Keep up the very super task and I’m going to be checking again yet again relatively quickly.

  • [...] Anita Boerboom has a great blog showing you how to use this. – HERE [...]

  • Heerak Vyas:

    Hi Anita,

    Thanks for such blog, it gives all information. Here i have one some quetsion.
    1) can we delete current magor and minor version, so the earlier version can become current version.
    2) I only get this on this issue is that we need to restore previous version and need to create a new version.
    3) Is there a way to delete faulty version???

    Waiting for your response.

    Thanks,
    Heerak

Leave a Reply


6 + = thirteen