SectionCollection Object (JavaScript API for OneNote)
Applies to: OneNote Online
Represents a collection of sections.
Properties
| Property | Type | Description | Feedback |
|---|---|---|---|
| count | int | Returns the number of sections in the collection. Read-only. | Go |
| items | Section[] | A collection of section objects. Read-only. | Go |
See property access examples.
Relationships
None
Methods
| Method | Return Type | Description | Feedback |
|---|---|---|---|
| getByName(name: string) | SectionCollection | Gets the collection of sections with the specified name. | Go |
| getItem(index: number or string) | Section | Gets a section by ID or by its index in the collection. Read-only. | Go |
| getItemAt(index: number) | Section | Gets a section 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
getByName(name: string)
Gets the collection of sections with the specified name.
Syntax
sectionCollectionObject.getByName(name);
Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | The name of the section. |
Returns
Examples
OneNote.run(function (context) {
// Get the sections in the current notebook.
var sections = context.application.getActiveNotebook().sections;
// Queue a command to load the sections.
// For best performance, request specific properties.
sections.load("id");
// Get the sections with the specified name.
var groceriesSections = sections.getByName("Groceries");
// Queue a command to load the sections with the specified name.
groceriesSections.load("id,name");
// Run the queued commands, and return a promise to indicate task completion.
return context.sync()
.then(function () {
// Iterate through the collection or access items individually by index.
if (groceriesSections.items.length > 0) {
console.log("Section name: " + groceriesSections.items[0].name);
console.log("Section ID: " + groceriesSections.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 section by ID or by its index in the collection. Read-only.
Syntax
sectionCollectionObject.getItem(index);
Parameters
| Parameter | Type | Description |
|---|---|---|
| index | number or string | The ID of the section, or the index location of the section in the collection. |
Returns
getItemAt(index: number)
Gets a section on its position in the collection.
Syntax
sectionCollectionObject.getItemAt(index);
Parameters
| Parameter | Type | Description |
|---|---|---|
| index | number | Index value of the object to be retrieved. Zero-indexed. |
Returns
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 sections in the current notebook.
var sections = context.application.getActiveNotebook().sections;
// Queue a command to load the sections.
// For best performance, request specific properties.
sections.load("name");
// Run the queued commands, and return a promise to indicate task completion.
return context.sync()
.then(function () {
// Iterate through the collection or access items individually by index, for example: sections.items[0]
$.each(sections.items, function(index, section) {
if (section.name === "Homework") {
section.addPage("Biology");
section.addPage("Spanish");
section.addPage("Computer Science");
}
});
return context.sync();
});
})
.catch(function(error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});