Copying documents between SharePoint libraries keeping version history (part 2)

Quick update on my exploits so far in trying to keep document version history when copying documents between SharePoint libraries (i.e. what I wrote in the title!!)

Good progress has been made from the starting point I mentioned in part 1. I have had to make a few changes to deal with the fact my libraries have major and minor versioning, which started me exploring the properties and methods available in SharePoint’s object model. To explore this further, I knocked up a noddy application which would allow me to experiment without going through the faff of having to deploy and trawl through my K2 workflow all the time, and in that I found some useful stuff for handling versions:

SPFile.Level is an enumerated property which indicates whether a version is draft, published or just checked out.
SPFile.CheckOutStatus indicates the nature of the checkout (long or short term), and so can also be used to determine if a file is checked out.
SPFile.UIVersion gives the 32-bit integer version number, whereas SPFile.UIVersionLabel returns the ‘user-friendly’ version number (e.g. 1.0, 1.1 etc…). Properties are also available that return just the major or minor part of the label, if that’s all you need to work with. (Note: when working with SPFile.Versions, use SPFile.Versions.ID and SPFile.Versions.VersionLabel respectively.)

So at this point it is easy to handle the different versions as necessary. The next hurdle is, well, an odd shaped one and I will hopefully find a reason and solution for it in due course. For some reason, I am having issues with handling the new Office 2007 format files. For example, versions of an old .doc (Office 2003) format file is fine, but a new .docx (2007) format file throws an error. I haven’t got the exact message to hand, but will grab it and post it later in case someone can tell me where I am going wrong! So for the moment, I have ignored this one, as most people who use this system are using Office 2003 so it will not be a great issue.

And so I will wrap up this post with a quick mention of checking in and out documents as part of the process. In dink’s example in part 1, the versions just get published, but as I said I need to handle minor and major versions. There is a minor check you need to do to see if the document is new in the destination library (my code copies new versions over existing documents as well!), and if it is, then you don’t need to check out first, you can just save. I should also point out that my libraries both have minor and major versioning, and require check in/out. Checking out is quite simple, just use SPFile.CheckOut(), and checking in is equally simple with the SPFile.CheckIn(String, SPCheckInType). SPCheckInType allows you to specify whether it is a minor or a major version.

This post is quite long enough already, so in part 3 I shall pop up some code (I can’t put it all up as it is technically proprietary, but then again this stuff isn’t rocket science!).



3 Responses to “Copying documents between SharePoint libraries keeping version history (part 2)”

  1. […] stuff…. a colleague of mine, has also published his ideas on a workaround to this issue: here. I just can’t believe Microsoft let this one exist in the final […]

  2. Daniel Westerdale Says:

    Did you every get around to Part 3 of this interesting blog. I thought I could use a VersionCopy class but I am strugging with the correnct context…..So just wondered which approach you used.

    • Sorry Daniel, I never got round to doing part 3 as I got re-assigned to work on a different (non-SharePoint) project. As such, I haven’t done any SharePoint development in a couple of years. I don’t recall a VersionCopy class, perhaps this has been added as part of a service pack update??

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: