Tuesday, 26 June 2018

Office ContentControls List Inaccurate

I can do this a couple of ways, but let's stick to the AddIn itself. If I create a ContentControl:

Word.run((context) => {
    let control = context.document.getSelection().insertContentControl();
    control.tag = 'example';
    control.insertOoxml('<xml here>');
    context.sync();
});

Then later (with proper async handling of course) I delete the control:

Word.run((context) => {
    let contentControls = context.document.contentControls;
    context.load(contentControls, 'tag');
    context.sync().then(() => {
        for (let c = 0; c < contentControls.items.length; ++c) {
            if (contentControls.items[c].tag === 'example') {
                contentControls.items[c].delete(false); // delete the contentControl with the matching tag
            }
        }
        context.sync()
    });
});

Then I check the list of content controls for that control I just deleted:

Word.run((context) => {
    let contentControls = context.document.contentControls;
    context.load(contentControls, 'tag');
    context.sync().then(() => {
        for (var i = 0; i < contentControls.items.length; ++i) {
            if (contentControls.items[c].tag === 'example') {
                // this tag list still includes the deleted content control until I close and reopen the document
            }
        }
    });
});

The tags still show the deleted content control. I have to close and reopen the document for the context to refresh. Am I missing a step for proper syncing with the current state of the document? Is context.sync() not enough?

NOTE: delete() does work: the content disappears from the document as expected. It's just still in the list of controls when I search the document.



from Office ContentControls List Inaccurate

No comments:

Post a Comment