Content Controls


Content Controls

Word.run(function (context) { 
    var contentControls = context.document.contentControls;
    contentControls.load('text');
     
    return context.sync().then(function () {
        
        if (contentControls.items.length === 0) {
            console.log("There isn't a content control in this document.");
        } else {
            contentControls.items[0].clear();

            return context.sync().then(function () {
                console.log('Content control cleared of contents.');
            });
        }
    });
})

Create a content control

Word.run(function (context) { 
    var range = context.document.getSelection();
    var myContentControl = range.insertContentControl();
    myContentControl.tag = 'Customer-Address';
    myContentControl.title = 'Enter Customer Address Here:';
    myContentControl.style = 'Heading 2';
    myContentControl.insertText('One Microsoft Way, Redmond, WA 98052', 'replace');
    myContentControl.cannotEdit = true;
    myContentControl.appearance = 'tags';
    context.load(myContentControl, 'id');
    
    return context.sync().then(function () {
        console.log('Created content control with id: ' + myContentControl.id);
    });
})

get all content controls

Word.run(function (context) { 
    var contentControls = context.document.contentControls;
    context.load(contentControls, 'id');
     
    return context.sync().then(function () {
        for (var i = 0; i < contentControls.items.length; i++) {
            console.log("contentControl[" + i + "].id = " +
                        contentControls.items[i].id);
        }
    });
})

get html from a content control

Word.run(function (context) { 
    var contentControlsWithTag = context.document.contentControls.getByTag('Customer-Address');
    context.load(contentControlsWithTag, 'tag');
     
    return context.sync().then(function () {
        if (contentControlsWithTag.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            var html = contentControlsWithTag.items[0].getHtml();
            var ooxml = contentControls.items[0].getOoxml();
        
            return context.sync()
                .then(function () {
                    console.log('Content control HTML: ' + html.value);
            });
        }
    });
})

insert page break after content control

Word.run(function (context) { 
    var contentControls = context.document.contentControls;
    context.load(contentControls, 'id');
    
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            contentControls.items[0].insertBreak('page', "After");
            contentControls.items[0].insertHtml('<strong>HTML content inserted into the content control.</strong>', 'Start');
            contentControls.items[0].insertParagraph('Text of the inserted paragraph.', 'After');
            contentControls.items[0].insertText('Replaced text in the first content control.', 'Replace');
            
            return context.sync()
                .then(function () {
                    console.log('Inserted a page break after the first content control.');
            });
        }
    });
})

displaying content controls

Word.run(function (context) { 
    var contentControls = context.document.contentControls;
    context.load(contentControls, 'id');
     
    return context.sync().then(function () {
        if (contentControls.items.length === 0) {
            console.log('No content control found.');
        }
        else {
            contentControls.items[0].load( 'appearance,' +
'cannotDelete,' +
'cannotEdit,' +
'color,' +
'id,' +
'placeHolderText,' +
'removeWhenEdited,' +
'title,' +
'text,' +
'type,' +
'style,' +
'tag,' +
'font/size,' +
'font/name,' +
'font/color');
        
            return context.sync()
                .then(function () {
                    console.log('Property values of the first content control:' +
' ----- appearance: ' + contentControls.items[0].appearance +
' ----- cannotDelete: ' + contentControls.items[0].cannotDelete +
' ----- cannotEdit: ' + contentControls.items[0].cannotEdit +
' ----- color: ' + contentControls.items[0].color +
' ----- id: ' + contentControls.items[0].id +
' ----- placeHolderText: ' + contentControls.items[0].placeholderText +
' ----- removeWhenEdited: ' + contentControls.items[0].removeWhenEdited +
' ----- title: ' + contentControls.items[0].title +
' ----- text: ' + contentControls.items[0].text +
' ----- type: ' + contentControls.items[0].type +
' ----- style: ' + contentControls.items[0].style +
' ----- tag: ' + contentControls.items[0].tag +
' ----- font size: ' + contentControls.items[0].font.size +
' ----- font name: ' + contentControls.items[0].font.name +
' ----- font color: ' + contentControls.items[0].font.color);
            });
        }
    });
})

Inserts, updates and retrieves content controls

$("#insert-controls").click(() => tryCatch(insertContentControls)); 
$("#change-controls").click(() => tryCatch(modifyContentControls));
$("#setup").click(() => tryCatch(setup));

async function insertContentControls() {
  await Word.run(async (context) => {
    let paragraphs = context.document.body.paragraphs;
    paragraphs.load("$none"); // Don't need any properties; just wrap each paragraph with a content control.

    await context.sync();

    for (let i = 0; i < paragraphs.items.length; i++) {
      let contentControl = paragraphs.items[i].insertContentControl();
      if (i % 2 === 0) {
        contentControl.tag = "even";
      } else {
        contentControl.tag = "odd";
      }
    }
    console.log("Content controls inserted: " + paragraphs.items.length);

    await context.sync();
  });
}

async function modifyContentControls() {
  await Word.run(async (context) => {
    let evenContentControls = context.document.contentControls.getByTag("even");
    let oddContentControls = context.document.contentControls.getByTag("odd");
    evenContentControls.load("length");
    oddContentControls.load("length");

    await context.sync();

    for (let i = 0; i < evenContentControls.items.length; i++) {
      evenContentControls.items[i].set({
        color: "red",
        title: "Odd ContentControl #" + (i + 1),
        appearance: "Tags"
      });
      evenContentControls.items[i].insertParagraph("This is an odd content control", "End");
    }

    for (let j = 0; j < oddContentControls.items.length; j++) {
      oddContentControls.items[j].set({
        color: "green",
        title: "Even ContentControl #" + (j + 1),
        appearance: "Tags"
      });
      oddContentControls.items[j].insertHtml("This is an <b>even</b> content control", "End");
    }

    await context.sync();
  });
}

async function setup() {
  await Word.run(async (context) => {
    context.document.body.clear();
    context.document.body.insertParagraph("One more paragraph. ", "Start");
    context.document.body.insertParagraph("Inserting another paragraph. ", "Start");
    context.document.body.insertParagraph(
      "Video provides a powerful way to help you prove your point.",
      "Start"
    );
    context.document.body.paragraphs
      .getLast()
      .insertText(
        "To make your document look professionally produced.",
        "Replace"
      );
  });
}

async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    console.error(error);
  }
}


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext