Documents and versioning

13 Jun

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.

34 Replies to “Documents and versioning

  1. 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!

  2. I had errors viewing the website in Firefox on the Mac, but apart from that loved the site! 🙂

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

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

  5. 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

  6. 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 .

  7. 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.

  8. 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.

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

  10. 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!

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

  12. 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!

  13. 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.

  14. 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…

  15. 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.

  16. 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.

  17. Pingback: Tim Ferro » Developer Tips From the MCTS 70-573 Exam

  18. 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

Comments are closed.