Skip to content
Chad Shurtz edited this page Aug 30, 2016 · 1 revision

This page assumes that your application has specified the necessary permissions for performing the below operations on the user's OneNote data. For more information on resources, see OneNote authentication and permissions.

The OneNoteApi object provides simple wrapper methods that allow you to read and write to a user's data on OneNote. These methods typically return a ResponsePackage object, encapsulating both the data returned by the REST API as well as the XHR itself. The data can be found in responsePackage.parsedResponse, while the XHR can be found in responsePackage.request.

For more information on return types, see structuredTypes.ts:

Creating notebooks

The supplied name must not include the following character literals ?*\/:<>"|. The notebook name must not exceed 50 characters.

api.createNotebook("New Notebook").then(function(responsePackage) {
    // The newly created notebook (of type Notebook)
    var notebook = responsePackage.parsedResponse.value;
});

Creating pages

// The library provides an easy way to create pages to be added to OneNote
var page = new OneNoteApi.OneNotePage("Title");
page.addOnml("<p>Hello world!</p>");
page.addImage(imgUrl);

// A list of section ids can be retrieved from the getNotebooksWithExpandedSections call
api.createPage(page, "0-EB15C30456630CBA!12345" /* sectionId */).then(function(responsePackage) {
    // The newly created page (of type Page)
    var newPage = responsePackage.parsedResponse.value;
});

Creating sections

// A list of notebook ids can be retrieved from the getNotebooks or getNotebooksWithExpandedSections call
api.createSection("0-EB15B30554412CBE!140" /* notebookId */, "New Section").then(function(responsePackage) {
    // The newly created section (of type Section)
    var newSection = responsePackage.parsedResponse.value;
}); 

Getting notebooks

api.getNotebooks().then(function(responsePackage) {
    // List of the user's notebooks (of type Notebook[])
    var notebooks = responsePackage.parsedResponse.value;
    var notebook = notebooks[0];
    console.log(notebook.sections === undefined); // true
    console.log(notebook.sectionGroups === undefined); // true
});

Getting notebooks with expanded sections

// Defaults to 2-levels deep, and excludes read-only notebooks
api.getNotebooksWithExpandedSections().then(function(responsePackage) {
    // List of the user's notebooks (of type Notebook[])
    var notebooks = responsePackage.parsedResponse.value;
    var notebook = notebooks[0];
    var sections = notebook.sections; // List of this notebook's sections (of type Section[])
    var sectionGroups = notebook.sectionGroups; // List of this notebook's section groups (of type SectionGroup[])
});

// You can also specify maximum section/section group depth, and if you want to exclude read-only notebooks or not
api.getNotebooksWithExpandedSections(5 /* expands */, false /* excludeReadOnlyNotebooks */);

Getting a notebook by name

api.getNotebookByName("My Notebook").then(function(responsePackage) {
    // The notebook with name "My Notebook" (of type Notebook)
    var notebook = responsePackage.parsedResponse.value;
});

api.getNotebookByName("Does not exist").then(function(responsePackage) {
    console.log(responsePackage.parsedResponse.value === undefined); // true
});

Searching pages with a query

The query provided is a term or phrase to search for in the page title, page body, image alt text, and image OCR text. Pages returned are sorted by relevance in descending order.

api.pagesSearch("The quick brown fox").then(function(responsePackage) {
    // Pages where the supplied phrase was found (of type Page[])
    var pages = responsePackage.parsedResponse.value;
});