OneNote.Notebook class
Represents a OneNote notebook. Notebooks contain section groups and sections.
- Extends
Remarks
Properties
base |
The url of the site where this notebook is located. Read-only. |
client |
The client url of the notebook. Read-only. |
context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
id | Gets the ID of the notebook. Read-only. |
is |
True if the notebook isn't created by the user (i.e., 'Misplaced Sections'). |
name | Gets the name of the notebook. |
section |
The section groups in the notebook. |
sections | The sections of the notebook. |
Methods
add |
Adds a new section to the end of the notebook. |
add |
Adds a new section group to the end of the notebook. |
get |
Gets the REST API ID. |
load(options) | Queues up a command to load the specified properties of the object. You must call |
load(property |
Queues up a command to load the specified properties of the object. You must call |
load(property |
Queues up a command to load the specified properties of the object. You must call |
toJSON() | Overrides the JavaScript |
track() | Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across |
untrack() | Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call |
Property Details
baseUrl
The url of the site where this notebook is located. Read-only.
readonly baseUrl: string;
Property Value
string
Remarks
clientUrl
The client url of the notebook. Read-only.
readonly clientUrl: string;
Property Value
string
Remarks
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
id
Gets the ID of the notebook. Read-only.
readonly id: string;
Property Value
string
Remarks
isVirtual
True if the notebook isn't created by the user (i.e., 'Misplaced Sections').
readonly isVirtual: boolean;
Property Value
boolean
Remarks
name
Gets the name of the notebook.
readonly name: string;
Property Value
string
Remarks
sectionGroups
The section groups in the notebook.
readonly sectionGroups: OneNote.SectionGroupCollection;
Property Value
Remarks
sections
The sections of the notebook.
readonly sections: OneNote.SectionCollection;
Property Value
Remarks
Method Details
addSection(name)
Adds a new section to the end of the notebook.
addSection(name: string): OneNote.Section;
Parameters
- name
-
string
The name of the new section.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
// Gets the active notebook.
const notebook = context.application.getActiveNotebook();
// Queue a command to add a new section.
const section = notebook.addSection("Sample section");
// Queue a command to load the new section. This example reads the name property later.
section.load("name");
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
console.log("New section name is " + section.name);
});
addSectionGroup(name)
Adds a new section group to the end of the notebook.
addSectionGroup(name: string): OneNote.SectionGroup;
Parameters
- name
-
string
The name of the new section.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
// Gets the active notebook.
const notebook = context.application.getActiveNotebook();
// Queue a command to add a new section group.
const sectionGroup = notebook.addSectionGroup("Sample section group");
// Queue a command to load the new section group.
sectionGroup.load();
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
console.log("New section group name is " + sectionGroup.name);
});
getRestApiId()
Gets the REST API ID.
getRestApiId(): OfficeExtension.ClientResult<string>;
Returns
OfficeExtension.ClientResult<string>
Remarks
Examples
await OneNote.run(async (context) => {
// Get the current notebook.
const notebook = context.application.getActiveNotebook();
const restApiId = notebook.getRestApiId();
await context.sync();
console.log("The REST API ID is " + restApiId.value);
// Note that the REST API ID isn't all you need to interact with the OneNote REST API.
// This is only required for SharePoint notebooks. baseUrl will be null for OneDrive notebooks.
// For SharePoint notebooks, the notebook baseUrl should be used to talk to the OneNote REST API
// according to the OneNote Development Blog.
// https://learn.microsoft.com/archive/blogs/onenotedev/and-sharepoint-makes-three
});
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(options?: OneNote.Interfaces.NotebookLoadOptions): OneNote.Notebook;
Parameters
Provides options for which properties of the object to load.
Returns
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(propertyNames?: string | string[]): OneNote.Notebook;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
Examples
await OneNote.run(async (context) => {
// Get the current notebook.
const notebook = context.application.getActiveNotebook();
// Queue a command to load the notebook.
// For best performance, request specific properties.
notebook.load('baseUrl');
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
console.log("Base url: " + notebook.baseUrl);
// This is only required for SharePoint notebooks, and will be null for OneDrive notebooks.
// This baseUrl should be used to talk to OneNote REST APIs according to the OneNote Development Blog.
// https://learn.microsoft.com/archive/blogs/onenotedev/and-sharepoint-makes-three
});
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): OneNote.Notebook;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select
is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand
is a comma-delimited string that specifies the navigation properties to load.
Returns
toJSON()
Overrides the JavaScript toJSON()
method in order to provide more useful output when an API object is passed to JSON.stringify()
. (JSON.stringify
, in turn, calls the toJSON
method of the object that's passed to it.) Whereas the original OneNote.Notebook
object is an API object, the toJSON
method returns a plain JavaScript object (typed as OneNote.Interfaces.NotebookData
) that contains shallow copies of any loaded child properties from the original object.
toJSON(): OneNote.Interfaces.NotebookData;
Returns
track()
Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync
calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created.
track(): OneNote.Notebook;
Returns
untrack()
Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync()
before the memory release takes effect.
untrack(): OneNote.Notebook;
Returns
Office Add-ins