Application Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents the top-level object that contains all globally addressable OneNote objects such as notebooks, the active notebook, and the active section.

Properties

None

Relationships

Relationship Type Description Feedback
notebooks NotebookCollection Gets the collection of notebooks that are open in the OneNote application instance. In OneNote Online, only one notebook at a time is open in the application instance. Read-only. Go

Methods

Method Return Type Description Feedback
getActiveNotebook() Notebook Gets the active notebook if one exists. If no notebook is active, throws ItemNotFound. Go
getActiveNotebookOrNull() Notebook Gets the active notebook if one exists. If no notebook is active, returns null. Go
getActiveOutline() Outline Gets the active outline if one exists, If no outline is active, throws ItemNotFound. Go
getActiveOutlineOrNull() Outline Gets the active outline if one exists, otherwise returns null. Go
getActivePage() Page Gets the active page if one exists. If no page is active, throws ItemNotFound. Go
getActivePageOrNull() Page Gets the active page if one exists. If no page is active, returns null. Go
getActiveSection() Section Gets the active section if one exists. If no section is active, throws ItemNotFound. Go
getActiveSectionOrNull() Section Gets the active section if one exists. If no section is active, returns null. Go
load(param: object) void Fills the proxy object created in JavaScript layer with property and object values specified in the parameter. Go
navigateToPage(page: Page) void Opens the specified page in the application instance. Go
navigateToPageWithClientUrl(url: string) Page Gets the specified page, and opens it in the application instance. Go

Method Details

getActiveNotebook()

Gets the active notebook if one exists. If no notebook is active, throws ItemNotFound.

Syntax

applicationObject.getActiveNotebook();

Parameters

None

Returns

Notebook

Examples

OneNote.run(function (context) {
        
    // Get the active notebook.
    var notebook = context.application.getActiveNotebook();
            
    // Queue a command to load the notebook. 
    // For best performance, request specific properties.           
    notebook.load('id,name');
            
    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
                    
            // Show some properties.
            console.log("Notebook name: " + notebook.name);
            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));
    }
});

getActiveNotebookOrNull()

Gets the active notebook if one exists. If no notebook is active, returns null.

Syntax

applicationObject.getActiveNotebookOrNull();

Parameters

None

Returns

Notebook

Examples

OneNote.run(function (context) {

    // Get the active notebook.
    var notebook = context.application.getActiveNotebookOrNull();

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

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

            // check if active notebook is set.
            if (!notebook.isNull) {
                console.log("Notebook name: " + notebook.name);
                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));
    }
});

getActiveOutline()

Gets the active outline if one exists, If no outline is active, throws ItemNotFound.

Syntax

applicationObject.getActiveOutline();

Parameters

None

Returns

Outline

Examples

OneNote.run(function (context) {

    // get active outline.
    var outline = context.application.getActiveOutline();

    // Queue a command to load the id of the outline.         
    outline.load('id');

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

            // Show some properties.
            console.log("outline id: " + outline.id);
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getActiveOutlineOrNull()

Gets the active outline if one exists, otherwise returns null.

Syntax

applicationObject.getActiveOutlineOrNull();

Parameters

None

Returns

Outline

Examples

OneNote.run(function (context) {

    // get active outline.
    var outline = context.application.getActiveOutlineOrNull();

    // Queue a command to load the id of the outline.         
    outline.load('id');

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

            if (!outline.isNull) {
                console.log("outline id: " + outline.id);
            }
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getActivePage()

Gets the active page if one exists. If no page is active, throws ItemNotFound.

Syntax

applicationObject.getActivePage();

Parameters

None

Returns

Page

Examples

OneNote.run(function (context) {
        
    // Get the active page.
    var page = context.application.getActivePage();
            
    // Queue a command to load the page. 
    // For best performance, request specific properties.           
    page.load('id,title');
            
    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
                    
            // Show some properties.
            console.log("Page title: " + page.title);
            console.log("Page ID: " + page.id);
            
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getActivePageOrNull()

Gets the active page if one exists. If no page is active, returns null.

Syntax

applicationObject.getActivePageOrNull();

Parameters

None

Returns

Page

Examples

OneNote.run(function (context) {

    // Get the active page.
    var page = context.application.getActivePageOrNull();

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

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            
            if (!page.isNull) {
                // Show some properties.
                console.log("Page title: " + page.title);
                console.log("Page ID: " + page.id);
            }
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getActiveSection()

Gets the active section if one exists. If no section is active, throws ItemNotFound.

Syntax

applicationObject.getActiveSection();

Parameters

None

Returns

Section

Examples

OneNote.run(function (context) {
        
    // Get the active section.
    var section = context.application.getActiveSection();
            
    // Queue a command to load the section. 
    // For best performance, request specific properties.           
    section.load('id,name');
            
    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
                    
            // Show some properties.
            console.log("Section name: " + section.name);
            console.log("Section ID: " + section.id);
            
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getActiveSectionOrNull()

Gets the active section if one exists. If no section is active, returns null.

Syntax

applicationObject.getActiveSectionOrNull();

Parameters

None

Returns

Section

Examples

OneNote.run(function (context) {

    // Get the active section.
    var section = context.application.getActiveSectionOrNull();

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

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            if (!section.isNull) {
                // Show some properties.
                console.log("Section name: " + section.name);
                console.log("Section ID: " + section.id);
            }
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

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

Opens the specified page in the application instance.

Syntax

applicationObject.navigateToPage(page);

Parameters

Parameter Type Description
page Page The page to open.

Returns

void

Examples

OneNote.run(function (context) {
        
    // Get the pages in the current section.
    var pages = context.application.getActiveSection().pages;
            
    // Queue a command to load the pages. 
    // For best performance, request specific properties.           
    pages.load('id');
            
    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
                    
            // This example loads the first page in the section.
            var page = pages.items[0];
                        
            // Open the page in the application.                    
            context.application.navigateToPage(page);
                    
            // Run the queued command.
            return context.sync();
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

Gets the specified page, and opens it in the application instance.

Syntax

applicationObject.navigateToPageWithClientUrl(url);

Parameters

Parameter Type Description
url string The client url of the page to open.

Returns

Page

Examples

OneNote.run(function (context) {

    // Get the pages in the current section.
    var pages = context.application.getActiveSection().pages;

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

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

            // This example loads the first page in the section.
            var page = pages.items[0];

            // Open the page in the application.                    
            context.application.navigateToPageWithClientUrl(page.clientUrl);

            // Run the queued command.
            return context.sync();
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});