Flexibility in a document generation system is very important to its usability. We all know how it works. You’ve been commissioned by the marketing department to put together a mailing to 50,000 customers. After doing the work of putting together the template document, the marketing department *will* come ask for changes to the data and to the template document. In the following screen cast, I show the process of adjusting the XML data that drives the document generation system, as well as adjusting the template document to use that data.
This post is the 12th in a series of blog posts on generating Open XML documents. Here is the complete list: Generating Open XML WordprocessingML Documents Blog Post Series
There are lots of disadvantages to this approach of editing C# code in content controls in a Word document:
- It requires a developer to put together the template document.
- If you write C# code that doesn’t compile in a content control, you don’t see any errors until you try to compile the generated program.
- There is no Intellisense when editing this code. In a couple of places, I ended up first getting a snippet of code to work in Visual Studio, and then pasting that code into the content control. This is far from ideal.
- The code that generates C# code from the template document is not long – only about 390 lines of code (see ProcessTemplate.cs in the Zip file). However, it is a bit gnarly, particularly the bits that make it so that you can have Value, Table, or Conditional content controls within a Conditional content control. However, the C# code that you write inside the template document is not so complex – just the code to generate the code.
There are advantages too:
- The code is directly associated and stored with the document. This is called ‘lexical proximity’ – you don’t need to find code in another file somewhere, and you don’t need to keep code and the template document in sync.
- You can pull data from *any* data source. I could easily modify the template document to use OData or the Managed Client Object Model to pull data from a SharePoint list. I could also write some ADO.NET code to pull data from any SQL database.
It is not clear that the advantages outweigh the disadvantages. In the next post in this series, I’m going to limit the data source to XML, and use XPath in the content controls.