PageCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of pages.

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

Properties

Property Type Description
count int Returns the number of pages in the collection. Read-only.
items Page[] A collection of page objects. Read-only.

See property access examples.

Relationships

None

Methods

Method Return Type Description
getByTitle(title: string) PageCollection Gets the collection of pages with the specified title.
getItem(index: number or string) Page Gets a page by ID or by its index in the collection. Read-only.
getItemAt(index: number) Page Gets a page 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

getByTitle(title: string)

Gets the collection of pages with the specified title.

Syntax

pageCollectionObject.getByTitle(title);

Parameters

Parameter Type Description
title string The title of the page.

Returns

PageCollection

Examples

OneNote.run(function (context) {

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

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

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

            // Get the sections with the specified name.
            var todoPages = allPages.getByTitle("Todo list");

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

            return context.sync()
                .then(function () {

                    // Iterate through the collection or access items individually by index.
                    if (todoPages.items.length > 0) {
                        console.log("Page title: " + todoPages.items[0].title);
                        console.log("Page ID: " + todoPages.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 page by ID or by its index in the collection. Read-only.

Syntax

pageCollectionObject.getItem(index);

Parameters

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

Returns

Page


getItemAt(index: number)

Gets a page on its position in the collection.

Syntax

pageCollectionObject.getItemAt(index);

Parameters

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

Returns

Page


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 pages in the current section.
    var pages = context.application.getActiveSection().pages;
    
    // Queue a command to load the id and title for each page.            
    pages.load('id,title');
    
    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {
            
            // Display the properties.
            $.each(pages.items, function(index, page) {
                console.log(page.title);
                console.log(page.id);
            });
        }); 
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
); // Run the queued commands, and return a promise to indicate task completion. return context.sync() .then(function () { // Display the properties. $.each(pages.items, function(index, page) { console.log(page.title); console.log(page.id); }); }); }) .catch(function(error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } });
TML Page Content End -->