ParagraphCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of Paragraph objects.

To provide feedback on this API, you can file an issue in GitHub.

Properties

Property Type Description
count int Returns the number of paragraphs on the page. Read-only.
items Paragraph[] A collection of paragraph objects. Read-only.

See property access examples.

Relationships

None

Methods

Method Return Type Description
getItem(index: number or string) Paragraph Gets a Paragraph object by ID or by its index in the collection. Read-only.
getItemAt(index: number) Paragraph Gets a paragraph on its position in the collection.
load(param: object) void Fills the proxy object created in the JavaScript layer with property and object values specified in the parameter.

Method details

getItem(index: number or string)

Gets a Paragraph object by ID or by its index in the collection. Read-only.

Syntax

paragraphCollectionObject.getItem(index);

Parameters

Parameter Type Description
index number or string The ID of the Paragraph object, or the index location of the Paragraph object in the collection.

Returns

Paragraph


getItemAt(index: number)

Gets a paragraph on its position in the collection.

Syntax

paragraphCollectionObject.getItemAt(index);

Parameters

Parameter Type Description
index number Index value of the object to be retrieved. Zero-indexed.

Returns

Paragraph

Examples

OneNote.run(function (context) {

    // Get the collection of pageContent items from the page.
    var pageContents = context.application.getActivePage().contents;

    // Get the first PageContent on the page, and then get its Outline's first paragraph.
    var pageContent = pageContents.getItemAt(0);
    var paragraphs = pageContent.outline.paragraphs;

    var firstParagraph = paragraphs.getItemAt(0);

    // Queue a command to load the type and richText.text property of this paragraph.
    firstParagraph.load("id,type");


    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            // Write text from paragraph to console
            console.log("First Paragraph found with id : " + firstParagraph.id + " and type " + firstParagraph.type);
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
}); 


load(param: object)

Fills the proxy object created in the JavaScript layer with property and object values specified in the parameter.

Syntax

object.load(param);

Parameters

Parameter Type Description
param object Optional. Accepts parameter and relationship names as a delimited string or an array. Or, provide loadOption object.

Returns

Void


Property access examples

items

OneNote.run(function (context) {

    // Get the collection of pageContent items from the page.
    var pageContents = context.application.getActivePage().contents;

    // Get the first PageContent on the page, and then get its Outline's first paragraph.
    var pageContent = pageContents.getItem(0);
    var paragraphs = pageContent.outline.paragraphs;
    
    // Queue a command to load the id and type of each paragraph.
    paragraphs.load("id,type");

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            var firstParagraph = paragraphs.items[0];
            // Write text from first paragraph to console
            console.log("First Paragraph found with id : " + firstParagraph.id + " and type " + firstParagraph.type);
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


traverse for richText

OneNote.run(function (context) {

    // Get the collection of pageContent items from the page.
    var pageContents = context.application.getActivePage().contents;

    // Get the first PageContent on the page, and then get its outline's paragraphs.
    var outlinePageContents = [];
    var paragraphs = [];
    var richTextParagraphs = [];
    // Queue a command to load the id and type of each page content in the outline.
    pageContents.load("id,type");

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            // Load all page contents of type Outline
            $.each(pageContents.items, function(index, pageContent) {
                if(pageContent.type == 'Outline')
                {
                    pageContent.load('outline,outline/paragraphs,outline/paragraphs/type');
                    outlinePageContents.push(pageContent);
                }
            });
            return context.sync();
        })
        .then(function () {
            // Load all rich text paragraphs across outlines
            $.each(outlinePageContents, function(index, outlinePageContent) {
                var outline = outlinePageContent.outline;
                paragraphs = paragraphs.concat(outline.paragraphs.items);
            });
            $.each(paragraphs, function(index, paragraph) {
                if(paragraph.type == 'RichText')
                {
                    richTextParagraphs.push(paragraph);
                    paragraph.load("id,richText/text");
                }
            });
            return context.sync();
        })
        .then(function () {
            // Display all rich text paragraphs to the console
            $.each(richTextParagraphs, function(index, richTextParagraph) {
                var richText = richTextParagraph.richText;
                console.log("Paragraph found with richtext content : " + richText.text + " and richtext id : " + richText.id);
            });
            return context.sync();
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


Text; console.log("Paragraph found with richtext content : " + richText.text + " and richtext id : " + richText.id); }); return context.sync(); }); }) .catch(function(error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } });
TML Page Content End -->