SectionCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of sections.

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

Properties

Property Type Description
count int Returns the number of sections in the collection. Read-only.
items Section[] A collection of section objects. Read-only.

See property access examples.

Relationships

None

Methods

Method Return Type Description
getByName(name: string) SectionCollection Gets the collection of sections with the specified name.
getItem(index: number or string) Section Gets a section by ID or by its index in the collection. Read-only.
getItemAt(index: number) Section Gets a section 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

getByName(name: string)

Gets the collection of sections with the specified name.

Syntax

sectionCollectionObject.getByName(name);

Parameters

Parameter Type Description
name string The name of the section.

Returns

SectionCollection

Examples

OneNote.run(function (context) {

    // Get the sections in the current notebook.
    var sections = context.application.getActiveNotebook().sections;

    // Queue a command to load the sections. 
    // For best performance, request specific properties.
    sections.load("id"); 
    
    // Get the sections with the specified name.
    var groceriesSections = sections.getByName("Groceries");
    
    // Queue a command to load the sections with the specified name.
    groceriesSections.load("id,name");

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {

            // Iterate through the collection or access items individually by index.
            if (groceriesSections.items.length > 0) {
                console.log("Section name: " + groceriesSections.items[0].name);
                console.log("Section ID: " + groceriesSections.items[0].id);
            }
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


getItem(index: number or string)

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

Syntax

sectionCollectionObject.getItem(index);

Parameters

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

Returns

Section


getItemAt(index: number)

Gets a section on its position in the collection.

Syntax

sectionCollectionObject.getItemAt(index);

Parameters

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

Returns

Section


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 sections in the current notebook.
    var sections = context.application.getActiveNotebook().sections;

    // Queue a command to load the sections. 
    // For best performance, request specific properties.
    sections.load("name"); 

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            
            // Iterate through the collection or access items individually by index, for example: sections.items[0]
            $.each(sections.items, function(index, section) {
                if (section.name === "Homework") {
                    section.addPage("Biology");
                    section.addPage("Spanish");
                    section.addPage("Computer Science");
                }
            });
            return context.sync();
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


s.items, function(index, section) { if (section.name === "Homework") { section.addPage("Biology"); section.addPage("Spanish"); section.addPage("Computer Science"); } }); 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 -->