NotebookCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of notebooks.

To provide feedback on this API, you can file an issue in GitHub.

Properties

Property Type Description
count int Returns the number of notebooks in the collection. Read-only.
items Notebook[] A collection of notebook objects. Read-only.

See property access examples.

Relationships

None

Methods

Method Return Type Description
getByName(name: string) NotebookCollection Gets the collection of notebooks with the specified name that are open in the application instance.
getItem(index: number or string) Notebook Gets a notebook by ID or by its index in the collection. Read-only.
getItemAt(index: number) Notebook Gets a notebook on its position in the collection.
load(param: object) void Fills the proxy object created in the JavaScript layer with property and object values specified in the parameter.

Method details

getByName(name: string)

Gets the collection of notebooks with the specified name that are open in the application instance.

Syntax

notebookCollectionObject.getByName(name);

Parameters

Parameter Type Description
name string The name of the notebook.

Returns

NotebookCollection

Examples

OneNote.run(function (context) {

    // Get the notebooks that are open in the application instance and have the specified name.
    var notebooks = context.application.notebooks.getByName("Homework");

    // Queue a command to load the notebooks. 
    // For best performance, request specific properties.           
    notebooks.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, for example: notebooks.items[0]
            if (notebooks.items.length > 0) {
                console.log("Notebook name: " + notebooks.items[0].name);
                console.log("Notebook ID: " + notebooks.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 notebook by ID or by its index in the collection. Read-only.

Syntax

notebookCollectionObject.getItem(index);

Parameters

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

Returns

Notebook


getItemAt(index: number)

Gets a notebook on its position in the collection.

Syntax

notebookCollectionObject.getItemAt(index);

Parameters

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

Returns

Notebook


load(param: object)

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

Returns

Void


Property access examples

items

OneNote.run(function (context) {

    // Get the notebooks that are open in the application instance and have the specified name.
    var notebooks = context.application.notebooks.getByName("Homework");

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

    // 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: notebooks.items[0]
            $.each(notebooks.items, function(index, notebook) {
                notebook.addSection("Biology");
                notebook.addSection("Spanish");
                notebook.addSection("Computer Science");
            });
            
            return context.sync();
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
individually by index, for example: notebooks.items[0] $.each(notebooks.items, function(index, notebook) { notebook.addSection("Biology"); notebook.addSection("Spanish"); notebook.addSection("Computer Science"); }); return context.sync(); }); }) .catch(function(error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } });
TML Page Content End -->