OneNote.Section class

Represents a OneNote section. Sections can contain pages.

Extends

Remarks

[ API set: OneNoteApi 1.1 ]

Properties

clientUrl

The client url of the section. Read only

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

id

Gets the ID of the section. Read-only.

isEncrypted

True if this section is encrypted with a password. Read only

isLocked

True if this section is locked. Read only

name

Gets the name of the section. Read-only.

notebook

Gets the notebook that contains the section. Read-only.

pages

The collection of pages in the section. Read only

parentSectionGroup

Gets the section group that contains the section. Throws ItemNotFound if the section is a direct child of the notebook. Read-only.

parentSectionGroupOrNull

Gets the section group that contains the section. Returns null if the section is a direct child of the notebook. Read-only.

webUrl

The web url of the page. Read only

Methods

addPage(title)

Adds a new page to the end of the section.

copyToNotebook(destinationNotebook)

Copies this section to specified notebook.

copyToSectionGroup(destinationSectionGroup)

Copies this section to specified section group.

getRestApiId()

Gets the REST API ID.

insertSectionAsSibling(location, title)

Inserts a new section before or after the current section.

insertSectionAsSibling(locationString, title)

Inserts a new section before or after the current section.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original OneNote.Section object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.SectionData) that contains shallow copies of any loaded child properties from the original object.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you needed to have added the object to the tracked object collection when the object was first created.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call context.sync() before the memory release takes effect.

Property Details

clientUrl

The client url of the section. Read only

readonly clientUrl: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

id

Gets the ID of the section. Read-only.

readonly id: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

isEncrypted

True if this section is encrypted with a password. Read only

readonly isEncrypted: boolean;

Property Value

boolean

Remarks

[ API set: OneNoteApi 1.2 ]

isLocked

True if this section is locked. Read only

readonly isLocked: boolean;

Property Value

boolean

Remarks

[ API set: OneNoteApi 1.2 ]

name

Gets the name of the section. Read-only.

readonly name: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

notebook

Gets the notebook that contains the section. Read-only.

readonly notebook: OneNote.Notebook;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

pages

The collection of pages in the section. Read only

readonly pages: OneNote.PageCollection;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

parentSectionGroup

Gets the section group that contains the section. Throws ItemNotFound if the section is a direct child of the notebook. Read-only.

readonly parentSectionGroup: OneNote.SectionGroup;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

parentSectionGroupOrNull

Gets the section group that contains the section. Returns null if the section is a direct child of the notebook. Read-only.

readonly parentSectionGroupOrNull: OneNote.SectionGroup;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

webUrl

The web url of the page. Read only

readonly webUrl: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

Method Details

addPage(title)

Adds a new page to the end of the section.

addPage(title: string): OneNote.Page;

Parameters

title

string

The title of the new page.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
            
    // Queue a command to add a page to the current section.
    const page = context.application.getActiveSection().addPage("Wish list");
            
    // Queue a command to load the id and title of the new page.
    // This example loads the new page so it can read its properties later.
    page.load('id,title');
            
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
             
    // Display the properties.
    console.log("Page name: " + page.title);
    console.log("Page ID: " + page.id);
});

copyToNotebook(destinationNotebook)

Copies this section to specified notebook.

copyToNotebook(destinationNotebook: OneNote.Notebook): OneNote.Section;

Parameters

destinationNotebook
OneNote.Notebook

The notebook to copy this section to.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    
    // Gets the active Notebook.
    const notebook = app.getActiveNotebook();
    
    // Gets the active Section.
    const section = app.getActiveSection();
    
    let newSection;
    
    await context.sync();

    newSection = section.copyToNotebook(notebook);
    newSection.load('id');
    await context.sync();
    
    console.log(newSection.id);
});

copyToSectionGroup(destinationSectionGroup)

Copies this section to specified section group.

copyToSectionGroup(destinationSectionGroup: OneNote.SectionGroup): OneNote.Section;

Parameters

destinationSectionGroup
OneNote.SectionGroup

The section group to copy this section to.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    
    // Gets the active Notebook.
    const notebook = app.getActiveNotebook();
    
    // Gets the active Section.
    const section = app.getActiveSection();
    
    let newSection;
    
    await context.sync();

    const firstSectionGroup = notebook.sectionGroups.items[0];
    newSection = section.copyToSectionGroup(firstSectionGroup);
    newSection.load('id');
    await context.sync();
    
    console.log(newSection.id);
});

getRestApiId()

Gets the REST API ID.

getRestApiId(): OfficeExtension.ClientResult<string>;

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    // Get the current section.
    const section = context.application.getActiveSection();
    const restApiId = section.getRestApiId();

    await context.sync();
    console.log("The REST API ID is " + restApiId.value);
    // Note that the REST API ID isn't all you need to interact with the OneNote REST API.
    // This is only required for SharePoint notebooks. baseUrl will be null for OneDrive notebooks.
    // For SharePoint notebooks, the notebook baseUrl should be used to talk to the 
    // OneNote REST API according to the OneNote Development Blog.
    // https://learn.microsoft.com/archive/blogs/onenotedev/and-sharepoint-makes-three
});

insertSectionAsSibling(location, title)

Inserts a new section before or after the current section.

insertSectionAsSibling(location: OneNote.InsertLocation, title: string): OneNote.Section;

Parameters

location
OneNote.InsertLocation

The location of the new section relative to the current section.

title

string

The name of the new section.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

insertSectionAsSibling(locationString, title)

Inserts a new section before or after the current section.

insertSectionAsSibling(locationString: "Before" | "After", title: string): OneNote.Section;

Parameters

locationString

"Before" | "After"

The location of the new section relative to the current section.

title

string

The name of the new section.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
            
    // Queue a command to insert a section after the current section.
    const section = context.application.getActiveSection().insertSectionAsSibling("After", "New section");
            
    // Queue a command to load the id and name of the new section.
    // This example loads the new section so it can read its properties later.
    section.load('id,name');
            
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
             
    // Display the properties.
    console.log("Section name: " + section.name);
    console.log("Section ID: " + section.id);
});

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(options?: OneNote.Interfaces.SectionLoadOptions): OneNote.Section;

Parameters

options
OneNote.Interfaces.SectionLoadOptions

Provides options for which properties of the object to load.

Returns

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): OneNote.Section;

Parameters

propertyNames

string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

Examples

await OneNote.run(async (context) => {
        
    // Get the current section.
    const section = context.application.getActiveSection();
            
    // Queue a command to load the section.
    // For best performance, request specific properties.
    section.load("id");
            
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
    console.log("Section ID: " + section.id);
});

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): OneNote.Section;

Parameters

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original OneNote.Section object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.SectionData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): OneNote.Interfaces.SectionData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you needed to have added the object to the tracked object collection when the object was first created.

track(): OneNote.Section;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call context.sync() before the memory release takes effect.

untrack(): OneNote.Section;

Returns