OneNote.Section class
Represents a OneNote section. Sections can contain pages.
- Extends
Remarks
Properties
client |
The client url of the section. |
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 section. |
is |
True if this section is encrypted with a password. |
is |
True if this section is locked. |
name | Gets the name of the section. |
notebook | Gets the notebook that contains the section. |
pages | The collection of pages in the section. |
parent |
Gets the section group that contains the section. Throws ItemNotFound if the section is a direct child of the notebook. |
parent |
Gets the section group that contains the section. Returns null if the section is a direct child of the notebook. Read-only. |
web |
The web URL of the page. |
Methods
add |
Adds a new page to the end of the section. |
copy |
Copies this section to specified notebook. |
copy |
Copies this section to specified section group. |
get |
Gets the REST API ID. |
insert |
Inserts a new section before or after the current section. |
insert |
Inserts a new section before or after the current section. |
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
clientUrl
The client url of the section.
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 section.
readonly id: string;
Property Value
string
Remarks
isEncrypted
True if this section is encrypted with a password.
readonly isEncrypted: boolean;
Property Value
boolean
Remarks
isLocked
True if this section is locked.
readonly isLocked: boolean;
Property Value
boolean
Remarks
name
Gets the name of the section.
readonly name: string;
Property Value
string
Remarks
notebook
Gets the notebook that contains the section.
readonly notebook: OneNote.Notebook;
Property Value
Remarks
pages
The collection of pages in the section.
readonly pages: OneNote.PageCollection;
Property Value
Remarks
parentSectionGroup
Gets the section group that contains the section. Throws ItemNotFound if the section is a direct child of the notebook.
readonly parentSectionGroup: OneNote.SectionGroup;
Property Value
Remarks
parentSectionGroupOrNull
Gets the section group that contains the section. Returns null if the section is a direct child of the notebook. Read-only.
readonly parentSectionGroupOrNull: OneNote.SectionGroup;
Property Value
Remarks
webUrl
The web URL of the page.
readonly webUrl: string;
Property Value
string
Remarks
Method Details
addPage(title)
Adds a new page to the end of the section.
addPage(title: string): OneNote.Page;
Parameters
- title
-
string
The title of the new page.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
// Queue a command to add a page to the current section.
const page = context.application.getActiveSection().addPage("Wish list");
// Queue a command to load the id and title of the new page.
// This example loads the new page so it can read its properties later.
page.load('id,title');
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
// Display the properties.
console.log("Page name: " + page.title);
console.log("Page ID: " + page.id);
});
copyToNotebook(destinationNotebook)
Copies this section to specified notebook.
copyToNotebook(destinationNotebook: OneNote.Notebook): OneNote.Section;
Parameters
- destinationNotebook
- OneNote.Notebook
The notebook to copy this section to.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
const app = context.application;
// Gets the active Notebook.
const notebook = app.getActiveNotebook();
// Gets the active Section.
const section = app.getActiveSection();
let newSection;
await context.sync();
newSection = section.copyToNotebook(notebook);
newSection.load('id');
await context.sync();
console.log(newSection.id);
});
copyToSectionGroup(destinationSectionGroup)
Copies this section to specified section group.
copyToSectionGroup(destinationSectionGroup: OneNote.SectionGroup): OneNote.Section;
Parameters
- destinationSectionGroup
- OneNote.SectionGroup
The section group to copy this section to.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
const app = context.application;
// Gets the active Notebook.
const notebook = app.getActiveNotebook();
// Gets the active Section.
const section = app.getActiveSection();
let newSection;
await context.sync();
const firstSectionGroup = notebook.sectionGroups.items[0];
newSection = section.copyToSectionGroup(firstSectionGroup);
newSection.load('id');
await context.sync();
console.log(newSection.id);
});
getRestApiId()
Gets the REST API ID.
getRestApiId(): OfficeExtension.ClientResult<string>;
Returns
OfficeExtension.ClientResult<string>
Remarks
Examples
await OneNote.run(async (context) => {
// Get the current section.
const section = context.application.getActiveSection();
const restApiId = section.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
});
insertSectionAsSibling(location, title)
Inserts a new section before or after the current section.
insertSectionAsSibling(location: OneNote.InsertLocation, title: string): OneNote.Section;
Parameters
- location
- OneNote.InsertLocation
The location of the new section relative to the current section.
- title
-
string
The name of the new section.
Returns
Remarks
insertSectionAsSibling(locationString, title)
Inserts a new section before or after the current section.
insertSectionAsSibling(locationString: "Before" | "After", title: string): OneNote.Section;
Parameters
- locationString
-
"Before" | "After"
The location of the new section relative to the current section.
- title
-
string
The name of the new section.
Returns
Remarks
Examples
await OneNote.run(async (context) => {
// Queue a command to insert a section after the current section.
const section = context.application.getActiveSection().insertSectionAsSibling("After", "New section");
// Queue a command to load the id and name of the new section.
// This example loads the new section so it can read its properties later.
section.load('id,name');
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
// Display the properties.
console.log("Section name: " + section.name);
console.log("Section ID: " + section.id);
});
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.SectionLoadOptions): OneNote.Section;
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.Section;
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 section.
const section = context.application.getActiveSection();
// Queue a command to load the section.
// For best performance, request specific properties.
section.load("id");
// Run the queued commands, and return a promise to indicate task completion.
await context.sync();
console.log("Section ID: " + section.id);
});
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.Section;
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.Section
object is an API object, the toJSON
method returns a plain JavaScript object (typed as OneNote.Interfaces.SectionData
) that contains shallow copies of any loaded child properties from the original object.
toJSON(): OneNote.Interfaces.SectionData;
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.Section;
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.Section;
Returns
Office Add-ins