PageCollection Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a collection of pages.

Properties

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

See property access examples.

Relationships

None

Methods

Method Return Type Description Feedback
getByTitle(title: string) PageCollection Gets the collection of pages with the specified title. Go
getItem(index: number or string) Page Gets a page by ID or by its index in the collection. Read-only. Go
getItemAt(index: number) Page Gets a page on its position in the collection. Go
load(param: object) void Fills the proxy object created in JavaScript layer with property and object values specified in the parameter. Go

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 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

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));
    }
});