Sadegh
Sadegh

Reputation: 4341

document.contentControls do not return all rich text content controls

I have an issue with content controls. There is 5 CCs in body itself, and 3 CCs inside a table inside the body.

This sample code just returns 5 CCs directly inside document body. I've ensured that all CCs are type of rich text content control.

Word.run(function (context) {
    const { document, document { body } } = context.document;

    context.load(document, 'contentControls');
    //Or context.load(body, 'contentControls');

    return context.sync().then(function () {
        const { contentControls } = document;
        //Or const { contentControls } = body;
         console.warn("len", contentControls.items.length);
    });
}).catch(function (e) {
    console.error(e);
});

Upvotes: 0

Views: 211

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13500

I'm unable to reproduce the issue that you describe. First, I completed the following setup steps:

  1. In a new document, add a few lines of text and a table.
  2. Add 3 content controls to the document:
    • add one content control to the first line of text
    • add one content control to the third line of text
    • add one content control inside the table (to the text in row 2, column 1 of the table)

Here's what that content looks like in the document:

enter image description here

After manually creating the document and adding content as described above, I ran this code snippet.

Word.run(function (context) {
    var myContentControls = context.document.contentControls;
    myContentControls.load("tag");

    return context.sync()
        .then(function () {
            for (var i = 0; i < myContentControls.items.length; i++)
            {
                myContentControls.items[i].color = "blue";
                myContentControls.items[i].title = "myCC";
                myContentControls.items[i].appearance = "tags";
            }
            return context.sync();
        });
}).catch(OfficeHelpers.Utilities.log);

This code snippet gets the content controls that the document contains and then sets tag properties for each one. As you can see from the following screenshot (which shows the document after the code snippet runs), all 3 content controls are successfully being identified, including the one that's inside the table.

enter image description here

Upvotes: 2

Related Questions