Word 2007 Content Controls

I really like the concept of content controls in Word 2007 for a couple of reasons; robustness and data-binding. They give you very granular control over which areas of a document can be modified, what content is allowed and whether those areas can be deleted.

Not only that you can map content controls to XML data (no need for a schema - just give me some XML data to bind to). This opens up some very interesting capabilities, especially if you combine them with the Document Information Panel (based on InfoPath) and harness InfoPath's data validation and connectivity. Very quickly you can build a rich dynamic document with no code behind. All the work is done by InfoPath and content controls...

Here's a quick overview - full references at the end:

  • Content controls are a little like XML “placeholder” elements in Word 2003 but more robust and they come in different flavours (plain text, date, image etc)
  • I can define what content they can contain and users are restricted to entering only that content (eg plain text, date, image etc)
  • I can specify if the control can be edited or deleted (making them much more robust as users can’t accidentally delete)
  • Out of the box we have support for mapping document and SharePoint properties directly to content controls. Try it by:
    • Open a Word 2007 document and go to the “Insert” tab
    • Drop down “Quick Parts”
    • Select (for example) “Property -> Title”
    • You now have a content control in the document called “Title” – the interesting thing is that the contents of this control are mapped to the document “Title” property
    • Type something into the content control and save the document
    • Close Word and look at the file properties – you’ll see the “Title” property set to whatever you entered in the content control
    • And this is a two way mapping. So modify the file properties and the changes are reflected back in the content control when you open the document!
  • This is supported for Word document properties and SharePoint properties
    • So if this was a Word document from a SharePoint document library with custom properties – these custom properties are automatically mapped to content controls available through the “Quick Parts” gallery
  • What’s even better than this though (can it get any better I hear you ask!), is you can add content controls to your document and map them to XML data
  • This takes advantage of two things – the custom XML store (I can push arbitrary XML data into an Open XML document) and data binding on the content controls using XPath to map to data in the custom XML store
  • You can set the XPath on content controls by automating Word and using the object model to set the XPath mapping. I wrote a simple VB.NET app to do this - it simply loops iterates the content controls in the doc and populates a Listview. I can then select any content control and specify the XPath I require
  • So, one very interesting scenario is generating documents on the server
    • I create a “template” document with the content controls and all the XPath mappings already in place
    • Using System.IO.Packaging (in .NET Fx 3.0) I replace the custom XML part in the template document with a part containing the data I want (retrieved from a database for example)
    • The content controls automatically bind to this data and I have a “new” document – the content controls take care of the data presentation for me
  • It’s also worth taking a look at Building Blocks as they’re a really nice solution for document assembly and can contain Content Controls so you can build complex document assembly solutions by combining the two

References

If people are interested I'll pull an article together and share the VB.NET app that I used to set the mappings. I'll also try and do something on the Document Information Panel and show how you can build a data-enabled, dynamic document with no code behind.