Search code examples
onenote-api

OneNote JS API - How to add a page to a specific section by title


I want to add a page to a specific section, but the documentation only gives examples of how to call the current page ie

context.application.getActiveSection().addPage;

But i cannot figure out for the life of me how I would choose a specific page by title. Any help would be much appreicated


Solution

  • You could get the active notebook, then the sections in the active notebook, then look for one with the name you're looking for.

    https://dev.office.com/reference/add-ins/onenote/notebook

    OneNote.run(function (context) {
        // Gets the active notebook.
        var notebook = context.application.getActiveNotebook();
    
        // Queue a command to get immediate child sections of the notebook. 
        var childSections = notebook.sections;
    
        // Queue a command to load the childSections. 
        context.load(childSections);
    
        // Run the queued commands, and return a promise to indicate task completion.
        return context.sync()
            .then(function() {
                $.each(childSections.items, function(index, childSection) {
                    console.log("Immediate child section name: " + childSection.name);
                });            
            });
    })
    .catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });