SectionGroupCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of section groups.

Properties

Property Type Description Feedback
count int Returns the number of section groups in the collection. Read-only. Go
items SectionGroup[] A collection of sectionGroup objects. Read-only. Go

See property access examples.

Relationships

None

Methods

Method Return Type Description Feedback
getByName(name: string) SectionGroupCollection Gets the collection of section groups with the specified name. Go
getItem(index: number or string) SectionGroup Gets a section group by ID or by its index in the collection. Read-only. Go
getItemAt(index: number) SectionGroup Gets a section group on its position in the collection. Go
load(param: object) void Fills the proxy object created in JavaScript layer with property and object values specified in the parameter. Go

Method Details

getByName(name: string)

Gets the collection of section groups with the specified name.

Syntax

sectionGroupCollectionObject.getByName(name);

Parameters

Parameter Type Description
name string The name of the section group.

Returns

SectionGroupCollection

Examples

OneNote.run(function (context) {

    // Get the section groups that are direct children of the current notebook.
    var sectionGroups = context.application.getActiveNotebook().sectionGroups;

    // Queue a command to load the section groups. 
    // For best performance, request specific properties.
    sectionGroups.load("id"); 

    // Get the section groups with the specified name.
    var labsSectionGroups = sectionGroups.getByName("Labs");

    // Queue a command to load the section groups with the specified properties.
    labsSectionGroups.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 (labsSectionGroups.items.length > 0) {
                console.log("Section group name: " + labsSectionGroups.items[0].name);
                console.log("Section group ID: " + labsSectionGroups.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 group by ID or by its index in the collection. Read-only.

Syntax

sectionGroupCollectionObject.getItem(index);

Parameters

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

Returns

SectionGroup

getItemAt(index: number)

Gets a section group on its position in the collection.

Syntax

sectionGroupCollectionObject.getItemAt(index);

Parameters

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

Returns

SectionGroup

load(param: object)

Fills the proxy object created in 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 delimited string or an array. Or, provide loadOption object.

Returns

void

Property access examples

items

OneNote.run(function (context) {

    // Get the section groups that are direct children of the current notebook.
    var sectionGroups = context.application.getActiveNotebook().sectionGroups;

    // Queue a command to load the section groups. 
    // For best performance, request specific properties.
    sectionGroups.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: sectionGroups.items[0]
            $.each(sectionGroups.items, function(index, sectionGroup) {
                console.log("Section group name: " + sectionGroup.name);  
                console.log("Section group ID: " + sectionGroup.id);  
            });
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});