Search code examples
c#ms-wordopenxmlopenxml-sdkword-2013

Word 2013 Nested Repeating Section Content Controls


I'm having a problem nesting repeating section controls within other repeating section controls.

Imagine I'm enumerating servers in vm hosts in environments in datacenters

Datacenter1
    Environment1
        VMHost1
           Server1
           Server2
        VMHost2
           Server3
           Server4
    Environment2
        VMHost3
           Server5
...

My document needs to have a number of repeating hierarchical sections.
This is the basic structure of the document:

Datacenter1 Header

    Some description text

    ----- Environment1 Table ---------
    | Header1  | Header 2 | Header 3 |
    |--------------------------------|
    | VMHost1  | Server1  | ........ |
    |          | Server2  | ........ |
    |--------------------------------|
    | VMHost2  | Server3  | ........ |
    |          | Server4  | ........ |
    ----------------------------------

    ----- Environment2 Table ---------
    | Header1  | Header 2 | Header 3 |
    |--------------------------------|
    | VMHost3  | Server5  | ........ |
    ----------------------------------

Datacenter2 Header

    Some description text

    ----- Environment3 Table ---------
    | Header1  | Header 2 | Header 3 |
    |--------------------------------|
    | VMHost1  | Server1  | ........ |
    |          | Server2  | ........ |
    |--------------------------------|
    | VMHost2  | Server3  | ........ |
    |          | Server4  | ........ |
    ----------------------------------

As you can see, I'll need a number of nested Content Controls.

But I get really funky behavior. Like when I add a nested repeating section, it sometimes removes the repeating control outside of it that is supposed to be wrapped around a table row.

Other times, I think I've got everything setup correctly, and then to test it I click the [+] to repeat the entire Datacenter section, but it does more than just repeat that section, or sometimes repeats other sections in the same section.

I'm not using the Grouping functionality - and unsure if I should or how it would help if I did.

Hopefully this is not a bug in the implementation, and I'm just doing something wrong .. but I'm worried this 'advanced' scenario is just not supported.

Ultimately, I will be locking all these controls down in the UI, and it will be populated using Custom XML Parts injected with the Open XML SDK. I have a number of other sections in the document - including repeating parts - which are working fine, but their repeating sections aren't nested.

I've already got the XML structure which should support the nesting, and as I worked my way down mapping these controls all was well, until I modified the XML to add more children to various nodes to test the repeating sections. In some cases it removed the entire sections, in others it repeated nested child nodes as parents, repeating entire tables.

I guess the questions are:
1. If you've successfully nested repeating sections, how?
2. If you've run into these funky behaviors, how did you work around it?
3. What is the purpose of the Grouping functionality with respect to sets of content controls, repeating sections, and should I be using them to accomplish this?


Solution

  • I've managed to get this working.
    Here is an example doc: http://1drv.ms/1nkMGVF
    I used this tool to help with naming, binding (AutoMap!), & navigating content controls.

    It looks like the problem was a combination of 2 things:

    1. Incompatible XML structure
    2. Mixing paragraph-based repeating sections within table-based repeating sections


    Incompatible XML
    To fix the problem with nested paragraph repeats in a cell in a repeating table row, I was wrapping repeating elements with an outer element:

    <relativeRoot>
        <nonRepeatingNode/>
        <table>
            <repeatingTableRow>
                <text1/>
                <text2/>
            </repeatingTableRow>
            <repeatingTableRow>
                <text1/>
                <text2/>
            </repeatingTableRow>
        </table>
    </relativeRoot>
    

    But this then caused problems when adding repeating elements via XML, or using the built-in Word functionality to add a repeating section. So I changed it to the following(removed <table>):

    <relativeRoot>
        <nonRepeatingNode/>
        <repeatingTableRow>
            <text1/>
            <text2/>
        </repeatingTableRow>
        <repeatingTableRow>
            <text1/>
            <text2/>
        </repeatingTableRow>
    </relativeRoot>
    


    Mixed Repeating Sections

    Repeating Sections can typically wrap paragraph text, but it appears when that repeating section is inside a table cell, whose row is wrapped with a repeating section, it causes problems with rendering the nested repeats.

    Here is a representation of the hierarchy before:

    repeating section control
    ^-> table
        ^-> row (fixed, non-repeating)
            ^-> column1a: plain text control
            ^-> column2a: table
                         ^-> repeating section control
                             ^-> row
                                 ^-> column1b: plain text control
                                 ^-> column2b: repeating section control
                                               ^--> plain text control
    
            ^-> column3a: repeating section control
                         ^-> plain text control
    
    
    - the repeats within column3a work
    - the repeats of row column1b/2b do not
    

    And here it is after:

    repeating section control
    ^-> table
        ^-> row (fixed, non-repeating)
            ^-> column1a: plain text control
            ^-> column2a: table
                          ^-> repeating section control
                              ^-> row
                                  ^-> column1b: plain text control
                                  ^-> column2b: table 
                                                ^-> repeating section control
                                                    ^-> row                                              
                                                        ^--> column1c: plain text control
    
            ^-> column3a: repeating section control
                         ^-> plain text control
    


    There may additional ways to make this work (check here), but for whatever reason I could not make it work.