SharePoint – Adding the document-version-history into MS Word

Office has a lot of nice features connected to SharePoint, one of them should be a way to insert the version history into your document. Unfortunately there’s no such thing. Maybe because MS wants you use SharePoint when you check the version history. But documents can be taken offline, moved, mailed or whatever and the person reading the document doesn’t necessary have access to SharePoint but still wants to know the story behind the document.

There is no fancy way to do this, not without an Office-Addin and nobody wants that, considering development hours and distribution to clients.

I had to figure out a way to get the document-version-history from SharePoint 2010 into MS Word using SharePoint/Office functionality. This is nothing standard and from what I have found, there is no easy way to do this.

What if I could use a Quick Part to show this? And since fields from SharePoint can be shown as Quick Parts in MS Word, this might be an easy way to do it!?

First I created a hidden field with PowerShell, a Note field with unlimited length and added it to the Content Types used by the documents. You can of course create a feature, console application or likewise to do this.

Do not use $field.Hidden = $true, this will cause the field to NOT appear as a Quick Part.

When this is done, you will have a field that will not be shown in SharePoint forms, but it will appear as a Quick Part in MS Word.

Here comes the tricky part.
When using a Quick Part, you can’t format the text, use rich text, use styles.. nothing. This means that we can’t create a layout for the history table. Therefor I decided to create a fake table using horizontal tabs and new lines. Not the prettiest way, but an acceptable way considering what we had to work with. One requirement was that the table should only contain major versions and draft versions of the current major.
ex. 1.0, 2.0, 3.0, 3.1, 3.2
Even if 1.1 and 1.2 existed, 2.0 should be the result of those drafts.

The fields that I used in the “table” was “Version, CreateTime, CreatedBy and CheckInComment. This order would make it easy to ensure a static length of the first 3 values since it will not be dynamic like a real table.

It would simply look lite this
“{0}t{1}t{2}t{3}n”

If the Check-In-Comment was to long, a length that would make it stretch over two lines, we would have to wrap it somehow. This was done by setting a max-length and adding horizontal tabs and newline where it was needed. As you can see in the code below.

You should be able to put the code in an event receiver or a workflow, as long as it triggers when the document is updated, we used event receiver ItemUpdated.

Creating the tab indented table.

How to wrap the comment if longer than the max-length.

Here is how the version history presents itself in SharePoint,

versionhistory

PowerShell, showing the field “VersionHistory”

versionhistorypowershell

and finally “Version History” as a Quick Part in MS Word. You should set the tab stop in the right position depending on your comment max-length. I also put a table on the top to make it look a little nicer.

versionhistoryinword

What you want to do now is add this Quick Part to your document template that you use in your Content Type, that way the version history Quick Part will appear in all new documents based on the Content Type.

About Tobias Eriksson