Dynamically generating Word 2010 documents using OpenXml and Content Controls

I was recently asked to write a Word 2010 Document Generator for a client where they had multiple text fields that needed to be dynamically filled with data from within SharePoint. I settled on using OpenXML (found here:  http://www.microsoft.com/download/en/details.aspx?id=5124) for the task and, though I’d never used it before, thought it looked very promising. Using Word I spun up my document with Plain Text Content Controls (found under the developers tag), after filling in the properties and giving my content controls titles I then dove into VS2010.

For those who don’t know, docx files can be converted to zip folders and you can view the XML inside which becomes very important for parsing and editting content controls. Basically OpenXml can read inside these folders and pull out information you need, so you essentially copy the stream of the original file, edit your bits and resave it\email it\do whatever with it.

So, I opened VS and added my docx file as a resource to the project then you open the docx file by using WordprocessingDocument.Open which gives you access to the word document content.

To get to the document part we are interested in for our content controls we need to go to WordprocessingDocument.MainDocumentPart.Document.

The important part to remember here was the title of the Content Control you created. When you create a content control, the title you give it is saved as an alias within the Xml. This is useful as it gives us a way of telling whether or not we have come across something we are interested in.

Elements also contain many other types inside of them, such as SdtContentBlock, SdtContentRun, Text, etc. However, for simplicities sake I knew the content blocks I was after contained a Text element.

So by iterating through all the elements and checking whether or not it has an alias allows us to find our content blocks. Once I had found that, I extracted it’s Text element and changed the value of it to something I wanted.

After I finished all this I then emailed the file.

Something worth noting is that we didn’t worry about the Content Controls not being removed after we edited them through code as the documents were only ever going to be printed as is, and we liked the way how it brought attention to the specifics we had changed.

Please download the sample code here to see a working example on how I did this.

About somethingsomethingsharepoint

A SharePoint dev for Intergen, a blog about my various discoveries and more interesting things during my work days.
This entry was posted in OpenXml, SharePoint, SharePoint 2010, Word Document Generation. Bookmark the permalink.

Leave a comment