OneNote.Notebook class

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

[ API set: OneNoteApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

baseUrl

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

[ API set: OneNoteApi 1.1 ]

clientUrl

The client url of the notebook. Read only

[ API set: OneNoteApi 1.1 ]

id

Gets the ID of the notebook. Read-only.

[ API set: OneNoteApi 1.1 ]

isVirtual

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

[ API set: OneNoteApi 1.2 ]

name

Gets the name of the notebook. Read-only.

[ API set: OneNoteApi 1.1 ]

sectionGroups

The section groups in the notebook. Read only

[ API set: OneNoteApi 1.1 ]

sections

The the sections of the notebook. Read only

[ API set: OneNoteApi 1.1 ]

Methods

addSection(name)

Adds a new section to the end of the notebook.

[ API set: OneNoteApi 1.1 ]

addSectionGroup(name)

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

[ API set: OneNoteApi 1.1 ]

getRestApiId()

Gets the REST API ID.

[ API set: OneNoteApi 1.1 ]

load(option)

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

toJSON()
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 that this notebook is located. Read only

[ API set: OneNoteApi 1.1 ]

readonly baseUrl: string;
Property Value
string

clientUrl

The client url of the notebook. Read only

[ API set: OneNoteApi 1.1 ]

readonly clientUrl: string;
Property Value
string

id

Gets the ID of the notebook. Read-only.

[ API set: OneNoteApi 1.1 ]

readonly id: string;
Property Value
string

isVirtual

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

[ API set: OneNoteApi 1.2 ]

readonly isVirtual: boolean;
Property Value
boolean

name

Gets the name of the notebook. Read-only.

[ API set: OneNoteApi 1.1 ]

readonly name: string;
Property Value
string

sectionGroups

The section groups in the notebook. Read only

[ API set: OneNoteApi 1.1 ]

readonly sectionGroups: OneNote.SectionGroupCollection;
Property Value

sections

The the sections of the notebook. Read only

[ API set: OneNoteApi 1.1 ]

readonly sections: OneNote.SectionCollection;
Property Value

Method Details

addSection(name)

Adds a new section to the end of the notebook.

[ API set: OneNoteApi 1.1 ]

addSection(name: string): OneNote.Section;
Parameters
name
string

The name of the new section.

Returns

Examples

OneNote.run(function (context) {

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

    // Queue a command to add a new section. 
    var 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.
    return context.sync()
        .then(function() {
            console.log("New section name is " + section.name);
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
}); 

addSectionGroup(name)

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

[ API set: OneNoteApi 1.1 ]

addSectionGroup(name: string): OneNote.SectionGroup;
Parameters
name
string

The name of the new section.

Returns

Examples

OneNote.run(function (context) {

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

    // Queue a command to add a new section group.
    var 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.
    return context.sync()
        .then(function() {
            console.log("New section group name is " + sectionGroup.name);
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
}); 

getRestApiId()

Gets the REST API ID.

[ API set: OneNoteApi 1.1 ]

getRestApiId(): OfficeExtension.ClientResult<string>;
Returns
OfficeExtension.ClientResult<string>

Examples

OneNote.run(function(ctx){
    // Get the current notebook.         
    var notebook = ctx.application.getActiveNotebook();
    var restApiId = notebook.getRestApiId();

    return ctx.sync().
        then(function(){
            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://blogs.msdn.microsoft.com/onenotedev/2015/06/11/and-sharepoint-makes-three/
        });
});

load(option)

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

load(option?: string | string[]): OneNote.Notebook;
Parameters
option
string | string[]

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

Returns
Remarks

In addition to this signature, this method has the following signatures:

load(option?: { select?: string; expand?: string; }): OneNote.Notebook - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): OneNote.Notebook - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

Examples

OneNote.run(function (context) {

    // Get the current notebook.
    var 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.
    return context.sync()
        .then(function () {
            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://blogs.msdn.microsoft.com/onenotedev/2015/06/11/and-sharepoint-makes-three/
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
OneNote.run(function (context) {

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

    // Queue a command to load the notebook. 
    // For best performance, request specific properties.           
    notebook.load('id');

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            console.log("Notebook ID: " + notebook.id);

        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
OneNote.run(function (context) {

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

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

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            console.log("Notebook name: " + notebook.name);

        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
OneNote.run(function (context) {

    // Get the section groups in the notebook. 
    var sectionGroups = context.application.getActiveNotebook().sectionGroups;

    // Queue a command to load the sectionGroups. 
    sectionGroups.load("name");

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function() {
            $.each(sectionGroups.items, function(index, sectionGroup) {
                console.log("Section group name: " + sectionGroup.name);
            });
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
OneNote.run(function (context) {

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

    // Queue a command to get immediate child sections of the notebook. 
    var childSections = notebook.sections;

    // Queue a command to load the childSections. 
    context.load(childSections);

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function() {
            $.each(childSections.items, function(index, childSection) {
                console.log("Immediate child section name: " + childSection.name);
            });            
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});   

toJSON()

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