1

As the title says I am trying to create a OneNote addin that just adds some text to the current page (in your OneNote Desktop App).

I've had a look at the API and I could add in the text to the Pages XML then update the page using UpdatePageContent()... but I can't see anything to give you the page you are currently looking at?

I'm sorry if this is obvious, or if there is a way to just edit the page without he need of the API... I've spent few days researching OneNote Addin examples but only just managed to actually get a button showing in the ribbon!

Any advice or guidance would be greatly appreciated.

Osie J O'Connor
  • 421
  • 7
  • 14

2 Answers2

1

Here's how you get the active page.

OneNote.run(function (context) {

    // Get the active page.
    var page = context.application.getActivePageOrNull();

    // Queue a command to load the page. 
    // For best performance, request specific properties.           
    page.load('id,title');

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .then(function () {

            if (!page.isNull) {
                // Show some properties.
                console.log("Page title: " + page.title);
                console.log("Page ID: " + page.id);
            }
        });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

https://github.com/OfficeDev/office-js-docs/blob/master/reference/onenote/application.md#getactivepageornull

And here's an example to add content to the active page:

OneNote.run(function (context) {

    // Gets the active page.
    var page = context.application.getActivePage();

    // Queue a command to add an outline with given html. 
    var outline = page.addOutline(200, 200,
"<p>Images and a table below:</p> \
 <img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"> \
 <img src=\"http://imagenes.es.sftcdn.net/es/scrn/6653000/6653659/microsoft-onenote-2013-01-535x535.png\"> \
 <table> \
   <tr> \
     <td>Jill</td> \
     <td>Smith</td> \
     <td>50</td> \
   </tr> \
   <tr> \
     <td>Eve</td> \
     <td>Jackson</td> \
     <td>94</td> \
   </tr> \
 </table>"     
        );

    // Run the queued commands, and return a promise to indicate task completion.
    return context.sync()
        .catch(function(error) {
            console.log("Error: " + error);
            if (error instanceof OfficeExtension.Error) {
                console.log("Debug info: " + JSON.stringify(error.debugInfo));
            }
        });
});
Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
  • Ok... So that wasn't the answer I needed because that is for the OneNote Web Application not the desktop one, but I have found what I wanted anyway. What I needed was a method hidden in the Windows part of the OneNote.Application object. – Osie J O'Connor Aug 01 '16 at 12:32
1

The answer if you are using the Desktop version of OneNote is this:

string thisNoteBook = oneNote.Windows.CurrentWindow.CurrentNotebookId;
string thisSection = oneNote.Windows.CurrentWindow.CurrentSectionId;    
string thisPage = oneNote.Windows.CurrentWindow.CurrentPageId;

These will get you the ID's of the page currently active, from there you can use

oneNote.GetPageContent(thisPage, out xmlPage);

or

oneNote.GetHierarchy(thisNoteBook, HierarchyScope.hsSections, out HieracyXML);

etc.


If anyone reading this needs any further help just send me a message.

Osie J O'Connor
  • 421
  • 7
  • 14
  • @samuel-oconnor Connected with this topic would you be able to help with this https://stackoverflow.com/questions/62272535/how-do-i-read-onenote-indented-paragraphs-using-onenote-javascript-apis ? – MiniMe Jun 10 '20 at 23:52