OneNote.Notebook class

Represents a OneNote notebook. Notebooks contain section groups and sections.

Extends

Remarks

[ API set: OneNoteApi 1.1 ]

Properties

baseUrl

The url of the site where this notebook is located. Read only

clientUrl

The client url of the notebook. 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 notebook. Read-only.

isVirtual

True if the notebook is not created by the user (i.e., 'Misplaced Sections'). Read only

name

Gets the name of the notebook. Read-only.

sectionGroups

The section groups in the notebook. Read only

sections

The sections of the notebook. Read only

Methods

addSection(name)

Adds a new section to the end of the notebook.

addSectionGroup(name)

Adds a new section group to the end of the notebook.

getRestApiId()

Gets the REST API 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(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.Notebook object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.NotebookData) 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

baseUrl

The url of the site where this notebook is located. Read only

readonly baseUrl: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

clientUrl

The client url of the notebook. 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 notebook. Read-only.

readonly id: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

isVirtual

True if the notebook is not created by the user (i.e., 'Misplaced Sections'). Read only

readonly isVirtual: boolean;

Property Value

boolean

Remarks

[ API set: OneNoteApi 1.2 ]

name

Gets the name of the notebook. Read-only.

readonly name: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

sectionGroups

The section groups in the notebook. Read only

readonly sectionGroups: OneNote.SectionGroupCollection;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

sections

The sections of the notebook. Read only

readonly sections: OneNote.SectionCollection;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

Method Details

addSection(name)

Adds a new section to the end of the notebook.

addSection(name: string): OneNote.Section;

Parameters

name

string

The name of the new section.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {

    // Gets the active notebook.
    const notebook = context.application.getActiveNotebook();

    // Queue a command to add a new section.
    const section = notebook.addSection("Sample section");
    
    // Queue a command to load the new section. This example reads the name property later.
    section.load("name");

    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
    console.log("New section name is " + section.name);
});

addSectionGroup(name)

Adds a new section group to the end of the notebook.

addSectionGroup(name: string): OneNote.SectionGroup;

Parameters

name

string

The name of the new section.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {

    // Gets the active notebook.
    const notebook = context.application.getActiveNotebook();

    // Queue a command to add a new section group.
    const sectionGroup = notebook.addSectionGroup("Sample section group");

    // Queue a command to load the new section group.
    sectionGroup.load();

    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
    console.log("New section group name is " + sectionGroup.name);
});

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 notebook.
    const notebook = context.application.getActiveNotebook();
    const restApiId = notebook.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
});

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.NotebookLoadOptions): OneNote.Notebook;

Parameters

options
OneNote.Interfaces.NotebookLoadOptions

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.Notebook;

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 notebook.
    const notebook = context.application.getActiveNotebook();
            
    // Queue a command to load the notebook.
    // For best performance, request specific properties.
    notebook.load('baseUrl');
            
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
    console.log("Base url: " + notebook.baseUrl);
    // This is only required for SharePoint notebooks, and will be null for OneDrive notebooks.
    // This baseUrl should be used to talk to OneNote REST APIs according to the OneNote Development Blog.
    // https://learn.microsoft.com/archive/blogs/onenotedev/and-sharepoint-makes-three
});

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.Notebook;

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.Notebook object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.NotebookData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): OneNote.Interfaces.NotebookData;

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.Notebook;

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.Notebook;

Returns