Recently, somebody asked me to add version history to word documents stored in Sharepoint. The word documents were docx files, based on the OpenXML format. This was a great opportunity to try the new OpenXML sdk 2.0 CTP (I’ve used a lot the Package & PackagePart classes in the past).
I decided to create an event handler overriding ItemCheckingIn method of the SPItemEventReceiver class and to a table in my OpenXml document (fig 1).
Fig 1.Overriding ItemCheckingIn and modifying the document
Then, after having associated my event handler with a document library, checking in a document generated an exception (fig 2)
Fig 2.Save Conflict exception when we update a list item during a Checkingin
To check my code, I’ve tried the following tests (without success):
- I’ve commented the AddTable() function –>same error
- Instead of Update, I’ve tried SystemUpdate() –>same error
Then I decided to override the CheckedIn event instead of the Checkingin (fig 3)
Fig 3. Trapping the ItemCheckedIn event
When I checked in, I got the usual window asking to to select the version (fig 4) ; here let’s select 0.2
Fig 4. Version selection , selecting 0.2
When I clicked on the document, indeed the version history shows up (fig 5).
Fig 5.Version history in the word document
But when I checked the version history, I’ve noticed that the system had created an additional draft version (fig 6).
Fig 6. version history: an additional draft version created.
The real culprit is the call to the SPFile::SaveBinary() function (see fig 3); it seems that there is no way to bypass it ! I’ve tried several things like removing the call to SaveBinary() and calling Flush() and Close() on the stream the file was not modified.
I hope that we will find a elegant way to achieve this in Sharepoint vNext.