locked
Identifying in which chapter a content control is contained RRS feed

  • Question

  • Hi,

    I'm working on a task pane addin for Word using the Javascript API for Office and have a requirement to identify which chapter a content control is contained in. A simplified verison of the issue is that I have two chapters in a document with style Heading1, so I have chapter 1 and chapter 2. I need to insert a content control in any given chapter, which works fine. But I also need to know which chapter the content control was inserted in, because I need this information for a summary.

    I insert the content control using this code within a Word.run->context.sync, which works perfectly.

    var selection = context.document.getSelection();
    var contentControl = selection.insertContentControl();
    contentControl.insertHtml(htmlContent, Word.InsertLocation.start);

    After the content control is inserted, the Word document looks like this.

    The corresponding ooxml paragraphs are (namespaces and revision tracking attributes omitted for simplicity):

    <w:p w14:paraId="442A333F" w14:textId="21823BC4">
      <w:pPr>
        <w:pStyle w:val="Heading1" />
      </w:pPr>
      <w:r>
        <w:t>Chapter 1</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="7C35E3D1" w14:textId="4E1BAF16">
      <w:r>
        <w:t>Chapter1 text</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="442A3342" w14:textId="6D58D67F">
      <w:pPr>
        <w:pStyle w:val="Heading1" />
      </w:pPr>
      <w:r>
        <w:t>Chapter 2</w:t>
      </w:r>
    </w:p>
    <w:sdt>
      <w:sdtPr>
        <w:alias w:val="SomeAlias" />
        <w:tag w:val="SomeTag" />
        <w:id w:val="1300341834" />
        <w:lock w:val="sdtLocked" />
        <w:placeholder>
          <w:docPart w:val="DefaultPlaceholder_-1854013440" />
        </w:placeholder>
      </w:sdtPr>
      <w:sdtContent>
        <w:p w14:paraId="0429A72C" w14:textId="5682C9A4">
          <w:pPr>
            <w:pStyle w:val="NormalWeb" />
          </w:pPr>
          <w:r>
            <w:t>Chapter2 content control text</w:t>
          </w:r>
        </w:p>
      </w:sdtContent>
    </w:sdt>

    So to summarize, I cannot find any functionality in the JS API for identifying the chapter which contains the content control. Is it possible to do this?

    Edit: Please note that I cannot perform this functionality on serverside using the documentformat.openxml API. The summary which need to include the chapter number should be dynamically updated in the Word document before it is saved in SharePoint.

    Best regards,

    Geir Morten Hagen


    Wednesday, January 3, 2018 7:47 AM

Answers

  • Hi Geir,

    >> But I also need to know which chapter the content control was inserted in, because I need this information for a summary

    What information did you need to summary?

    Let’s talk about your demo, how did you identify which chapter the content control was inserted in? Do you identify by string “Chapter2” in the Content Control?

    In my option, you could loop through ContentControlCollection Object (JavaScript API for Word), and access text property of ContentControl Object, then check text by your own logic.

    Not sure whether you need paragraphs, you could access paragraphs property by ContentControl Object.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 8:18 AM

All replies

  • Hi Geir Morten Hagen,

    I did not find any way or method that identify the chapter contains the content control or not.

    but if you need any specific content control then you can use getById(id: number) method.

    then you can insert the contents in specific content control and use it for summary.

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
    
        // Create a proxy object for the content control that contains a specific id.
        var contentControl = context.document.contentControls.getById(30086310);
    
        // Queue a command to load the text property for a content control.
        context.load(contentControl, 'text');
    
        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('The content control with that Id has been found in this document.');
        });
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });

    Reference:

    ContentControlCollection Object (JavaScript API for Word)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 4, 2018 8:50 AM
  • Hi,

    I've done some more investigation on this and it doesn't look like it is possible through the JS API.

    However, we're changing the user interface with a new button the user can click to update the summary with correct chapter numbers. We then send the ooxml of the body object to a web service and do the processing for finding chapter numbers. Not ideal, but a work-around at least.

    But I noticed something. Calling getOoxml() on the body object:

    return Word.run(function (context) {
    	var ooxml = context.document.body.getOoxml();
    	return context.sync().then(function () {
    		console.log("ooxml: " + ooxml.value);
    	});
    });
    

    The returned ooxml does not contain all paragraph attributes. For instance, I have one paragraph definition in the document (inspected by OpenXml Productivity Tool):

    <w:p w:rsidR="005E7506" w:rsidP="005E7506" w:rsidRDefault="005E7506" w14:paraId="432E775A" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:pPr>
        <w:pStyle w:val="Heading1" />
        <w:spacing w:before="0" w:after="0" />
      </w:pPr>
      <w:r>
        <w:lastRenderedPageBreak />
        <w:t>Chapter text</w:t>
      </w:r>
    </w:p>

    But the same paragraph definition returned from getOoxml() is missing paraId:

    <w:p w:rsidP="005E7506" w:rsidRDefault="00AC6F9B" w:rsidR="00AC6F9B">
    	<w:pPr>
    		<w:pStyle w:val="Heading1"/>
    		<w:spacing w:after="0" w:before="0"/>
    	</w:pPr>
    	<w:r>
    		<w:t>Chapter text</w:t>
    	</w:r>
    </w:p>

    I was thinking of using the Paragraph.ParagraphId property in DocumentFormat.OpenXml API when calculating chapters, but the property is null.

    Any ideas?

    Best regards,

    Geir Morten Hagen

    Wednesday, January 10, 2018 12:22 PM
  • Hi Geir,

    >> But I also need to know which chapter the content control was inserted in, because I need this information for a summary

    What information did you need to summary?

    Let’s talk about your demo, how did you identify which chapter the content control was inserted in? Do you identify by string “Chapter2” in the Content Control?

    In my option, you could loop through ContentControlCollection Object (JavaScript API for Word), and access text property of ContentControl Object, then check text by your own logic.

    Not sure whether you need paragraphs, you could access paragraphs property by ContentControl Object.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 8:18 AM
  • Thanks for your input!


    Wednesday, January 17, 2018 8:41 AM