Creating Data-Bound Content Controls using the Open XML SDK and LINQ to XML

Data-bound content controls are a powerful and convenient way to separate the semantic business data from the markup of an Open XML document.  After binding content controls to custom XML, you can query the document for the business data by looking in the custom XML part rather than examining the markup.  Querying custom XML is much simpler than querying the document body.  However, it’s a little bit involved to create data-bound content controls (but only a little bit).  But there is a trick we can do – we can take a document that has un-bound content controls, generate a custom XML part automatically (inferring the elements of the custom XML from the content controls), and then bind the content controls to the custom XML part.

This blog is inactive.
New blog:

Blog TOC(Update March 10, 2009 – modified code to work with latest Open XML SDK.) 

This approach has two benefits – first, it can serve as a way to conveniently create a document with data-bound content controls, and second, it serves to demonstrate exactly what you must do to create data-bound content controls.

This example uses the following approach:

  • Using Word 2007, you create a document with any number of content controls in it.
  • When creating each content control, you set the Tag of the content control to the desired XML element name in the custom XML.
  • You then run this example code on the document, which creates the custom XML part, creates the custom XML properties part, and then adds the markup to the body of the document that binds each content control to the custom XML.

This example uses the Open XML SDK V1 and LINQ to XML.

Data-Bound Content Controls

A document that contains properly set-up data-bound content control has the following characteristics:

  • The main document part has a relation to the custom XML part.
  • The custom XML part has a relation to a custom XML properties part.
  • The custom XML properties part contains a GUID in an attribute (ds:itemID).  This GUID is used to associate the data binding elements in the main document part to the relevant custom XML part.
  • Within the content control markup in the main document part, the data binding element (w:dataBinding) defines the data binding.  This element has an attribute (w:storeItemID) that contains the same GUID as in the custom XML properties part.  In addition, the element has an attribute (w:xpath) that contains the XPath expression to the relevant node in the custom XML.

The following screen clipping shows the word document with content controls in the cells of a table:

To set the properties of the content control, click on the Content Controls Properties button (on the Developer tab of the ribbon):

In this example, the element name in the custom XML part comes from the Tag field in the content control properties window:

The following screen clipping (using the Open XML Package Editor, which comes with Visual Studio Power Tools) shows that there is a relation from the main document part (document.xml) to the custom XML part (../customXml/item1.xml):

The following shows the relation from the custom XML part to the custom XML properties part (itemProps1.xml):

The custom XML for the example included with this post looks like this:

  <Name>Eric White</Name>
  <Company>Microsoft Corporation</Company>
  <Address>One Microsoft Way</Address>

This custom XML is automatically generated by this example.

The custom XML properties part looks like this:


The GUID in the ds:itemID attribute is generated when the example is run.

The content control with properly set-up data binding looks like this:

          <w:t>Eric White</w:t>

The GUID in the w:storeItemID attribute is the same as in the custom XML properties part.  This creates the association between the data-bound content control and its custom XML part.

If you edit the document that has bound content controls, and change the contents in one of them, the custom XML is modified to reflect the changed content.  For instance, if you edit the document and change the name to Tai Yee, then the custom XML will be:

  <Name>Tai Yee</Name>
  <Company>Microsoft Corporation</Company>
  <Address>One Microsoft Way</Address>

Because the GUID that creates the association is in the custom XML properties part and not in the custom XML itself, the custom XML can have any schema you desire.  You can take XML from any source, with any schema, and place it, unmodified, in a custom XML part, and create the appropriate data-binding to content controls.

Example using the Open XML SDK V1 and LINQ to XML

The example first copies Template.docx to Test.docx.  It opens Test.docx using the Open XML SDK, creates the custom XML part, creates the custom XML properties part, and then adds the data binding elements to the content controls in the main document part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;

public static class LocalExtensions
    public static string StringConcatenate<T>(this IEnumerable<T> source,
            Func<T, string> func)
        StringBuilder sb = new StringBuilder();
        foreach (T item in source)
        return sb.ToString();

    public static string StringConcatenate(this IEnumerable<string> source)
        StringBuilder sb = new StringBuilder();
        foreach (string item in source)
        return sb.ToString();

    public static XDocument GetXDocument(this OpenXmlPart part)
        XDocument xdoc = part.Annotation<XDocument>();
        if (xdoc != null)
            return xdoc;
        using (Stream str = part.GetStream())
        using (StreamReader streamReader = new StreamReader(str))
        using (XmlReader xr = XmlReader.Create(streamReader))
            xdoc = XDocument.Load(xr);
        return xdoc;

class Program
    private static XNamespace w =
    private static XName r = w + “r”;
    private static XName ins = w + “ins”;
    private static XNamespace ds =

    static string GetTextFromContentControl(XElement contentControlNode)
        return contentControlNode.Descendants(w + “p”)
                p => p.Elements()
                      .Where(z => z.Name == r || z.Name == ins)
                      .Descendants(w + “t”)
                      .StringConcatenate(element =>
                          (string)element) + Environment.NewLine

    static void Main(string[] args)
        File.Copy(“Template.docx”, “Test.docx”);

        // Open the Open XML doc as a word processing doc
        using (WordprocessingDocument document =
            WordprocessingDocument.Open(“Test.docx”, true))
            // Create the contents of the custom XML part
            XElement customXml = new XElement(“Root”,
                .Descendants(w + “sdt”)
                .Select(sdt =>
                    new XElement(
                        sdt.Element(w + “sdtPr”)
                            .Element(w + “tag”)
                            .Attribute(w + “val”).Value,

            // Create a new custom XML part
            CustomXmlPart customXmlPart =
            using (Stream str = customXmlPart.GetStream(
                FileMode.Create, FileAccess.ReadWrite))
            using (XmlWriter xw = XmlWriter.Create(str))

            Guid idGuid = Guid.NewGuid();

            // Create the contents of the properties part
            XDocument propertyPartXDoc = new XDocument(
                new XElement(ds + “datastoreItem”,
                    new XAttribute(ds + “itemID”,
                        “{“ + idGuid.ToString().ToUpper() + “}”),
                    new XAttribute(XNamespace.Xmlns + “ds”,
                    new XElement(ds + “schemaRefs”)

            // Add the custom XML properties part
            CustomXmlPropertiesPart customXmlPropertyPart =
            using (Stream str = customXmlPropertyPart.GetStream(
                FileMode.Create, FileAccess.ReadWrite))
            using (XmlWriter xw = XmlWriter.Create(str))

            // Load the main document part into an XDocument
            XDocument mainDocumentXDoc;
            using (Stream str = document.MainDocumentPart.GetStream())
            using (XmlReader xr = XmlReader.Create(str))
                mainDocumentXDoc = XDocument.Load(xr);

            // Add the data binding elements to the main document
            foreach (XElement sdt in mainDocumentXDoc.Descendants(w + “sdt”))
                sdt.Element(w + “sdtPr”)
                    .Element(w + “placeholder”)
                        new XElement(w + “dataBinding”,
                            new XAttribute(w + “xpath”,
                                “/Root/” + sdt.Element(w + “sdtPr”)
                                    .Element(w + “tag”)
                                    .Attribute(w + “val”).Value),
                            new XAttribute(w + “storeItemID”,
                                “{“ + idGuid.ToString().ToUpper() + “}”)

            // Serialize the XDocument back into the part
            using (Stream str = document.MainDocumentPart.GetStream(
                FileMode.Create, FileAccess.Write))
            using (XmlWriter xw = XmlWriter.Create(str))

Code is attached.

Comments (20)

  1. Doug Mahugh says:

    Stephen McGibbon has screenshots of the Open XML and ODF support coming in Windows 7 Wordpad , as announced

  2. Suite à la PDC 2008 et au workshop Open XML donné par Microsoft à Redmond ( Doug , encore mille excuses

  3. Sondre says:

    Question regarding the GetTextFromContentControl method in your example. This looks for "p" elements and there is normally (as far as I’ve seen) no "p" tags within the "sdt" elements, which is the parameter into the method.

    Looking at some of my own Open XML documents, it looks like the following example would be more correct. Yet, this example does not support placeholders that allows carriage returns.

    e.Element(w + "sdtContent").Element(w + "r").Element(w + "t").Value.Trim()

    Additionally the code will fail whenever there is placeholders that does not have any tag specified, to avoid this you can make a check in the foreach loops, something like:

    if (sdt.Element(w + "sdtPr").Element(w + "tag") != null)

    Thanks for a great example!

  4. Ryan Riley says:

    I just read Brian Jones’ <a href="; title="Taking Advantage of Bound Content Controls">post</a> where he completely swaps out the custom XML part. The code appears much more concise, but does it lack in the area of property reconstructing the Custom XML Part Properties?

  5. satchi says:

    Hi Eric,

    Can we do the custom binding for content controls that are in header and footer parts?

  6. Engr_Muneer says:

    Is there any way I can toggle the content control bordering and highlighting?

    I have some content controls that are very close together and they exhibit some really strange behavior.

  7. Darin says:

    Hi Eric

    Great blog, and good info on content controls here, but, I’m using Word 2007, and when I create a docx with one content control nested within another, save and then try to reload that document, Word throws an error, and offers to "correct the currupted document"

    When I click YES, the doc loads, but the nested content control has been stripped and converted to text.

    This is in a completely fresh doc on a system with a fresh install of office 2007, so I’m a bit stumped. Are nested content controls +really+ supported?


  8. EricWhite says:

    @Engr_Muneer, have you taken a look at “design mode” for content controls?  It can really help with how you interact with them.  Take a look at this post:


    I tried creating nested content controls using Word 2007, and it worked just fine for me.  I tried on multiple installs.  Can you try on some other Word 2007 installs, see if it works elsewhere?


  9. EricWhite says:

    @satchi, yes, you can link content controls in headers/footers to custom XML.  The XPath expression refers to elements/attributes in the custom XML part that is related to the main document part.


  10. Darin says:

    Very strange.

    I’m running word 12.0.4518.1000 (ie original shpping version from what I can tell).

    It definitely says that the doc has been corrupted once it’s saved and reloaded.

    I went to a colleague’s desk, he’s running 12.0.6500,5000 (it says it’s SP2) and his version works completely differently. No matter what we do at has desk, we can’t get it to insert a nested content control at all. The ribbon buttons for controls on the developer ribbon are greyed when the cursor is in a content control.


  11. Darin says:

    I’m running Win Update on this image now. Just have to see if maybe the Sp has something to do with it.

  12. Darin says:

    Aha! Finally figured out what’s up with this.

    Just fyi for anyone else that might come across this page,

    You can’t insert a content control into a "Plain text" content control. But it appears that you CAN insert a content control into a "Rich text" content control.

    I suppose that makes a certain amount of sense, but it sure wasn’t clear (and the older Word 2007 definitely would LET me do it, even though it appears that it shouldn’t have.

    And the controls do appear to save and reload properly, without Word stripping them out.

  13. EricWhite says:

    Thank you Darin for figuring this out.  This was one of those assumptions that was so ingrained in my mind that I forgot to mention.  I’m going to update the nested content control blog post to tell this.


  14. George Pivac says:

    Since the custom XML part is removed from Word from January 10…. Does anyone knows how to achieve content-controls/custom XML mapping in Word 2010? In other words, how it will be done in Word 2010? We are using method specified in this article for filling content controls from custom XML (Word 2007 – before January 10.), but how will we achieve that in Word 2010. I’m thinking about a future…

  15. EricWhite says:

    Hi George,

    Custom XML parts are not removed from Office 2007 or Office 2010.  Content controls are also not removed.  Binding content controls to custom XML in a custom XML part also is still supported.  The affected feature is ‘custom XML markup’, also known as ‘pink tags’.  See the following blog post for more info.


  16. Jo says:

    We had a document generation function based on the custom XML markup in word 2003 – the "pink tags" which I'm trying to work out how to convert so that the template documents can be maintained in word 2010. The big stumbling block is how to deal with nested data involving multiple rows – e.g. a document containing a customer with one or more orders each of which has one or more order items.  I can set up the nested custom controls but seem to be stuck with one order and one order item and I can't find any examples online dealing with this type of data. Can you give any guidance on this?

  17. James says:

    Hi Eric,

    I put the document generated from this example through the "Iterating through all Content Controls in an Open XML WordprocessingML Document" you wrote earlier and the contentcontrol tagged with "Name" is not detected. However if I move the "Name" contentcontrol to some other location it gets detected again. Can someone confirm this? Much appreciated.

  18. Mac1973 says:

    How would you remove default text "Click here to enter text." for the blank content controls?

  19. Martin Woolley says:

    Thanks very much for this article, I appreciate it's old now but I wonder if anyone can help me.

    It works fine for very simple documents, but once I put in some formatting, the document adds a customXML part with item1.xml and item1Props.xml. When I use this code, I find that it generates the item.xml with the data but fails to create the itemProps.xml so it doesn't get linked in.

    ANy thoughts anyone?