Build your first OneNote task pane add-in

In this article, you'll walk through the process of building a OneNote task pane add-in.

Prerequisites

  • Node.js (version 8.0.0 or later)

  • Git

  • The latest version of Yeoman and the Yeoman generator for Office Add-ins. To install these tools globally, run the following command via the command prompt:

    npm install -g yo generator-office
    

    Note

    Even if you've previously installed the Yeoman generator, we recommend you update your package to the latest version from npm.

Create the add-in project

  1. Use the Yeoman generator to create a OneNote add-in project. Run the following command and then answer the prompts as follows:

    yo office
    
    • Choose a project type: Office Add-in Task Pane project
    • Choose a script type: Javascript
    • What do you want to name your add-in? My Office Add-in
    • Which Office client application would you like to support? OneNote

    A screenshot of the prompts and answers for the Yeoman generator

    After you complete the wizard, the generator will create the project and install supporting Node components.

  2. Navigate to the root folder of the project.

    cd "My Office Add-in"
    

Explore the project

The add-in project that you've created with the Yeoman generator contains sample code for a very basic task pane add-in.

  • The ./manifest.xml file in the root directory of the project defines the settings and capabilities of the add-in.
  • The ./src/taskpane/taskpane.html file contains the HTML markup for the task pane.
  • The ./src/taskpane/taskpane.css file contains the CSS that's applied to content in the task pane.
  • The ./src/taskpane/taskpane.js file contains the Office JavaScript API code that facilitates interaction between the task pane and the Office host application.

Update the code

In your code editor, open the file ./src/taskpane/taskpane.js and add the following code within the run function. This code uses the OneNote JavaScript API to set the page title and add an outline to the body of the page.

try {
    await OneNote.run(async context => {

        // Get the current page.
        var page = context.application.getActivePage();

        // Queue a command to set the page title.
        page.title = "Hello World";

        // Queue a command to add an outline to the page.
        var html = "<p><ol><li>Item #1</li><li>Item #2</li></ol></p>";
        page.addOutline(40, 90, html);

        // Run the queued commands, and return a promise to indicate task completion.
        return context.sync();
    });
} catch (error) {
    console.log("Error: " + error);
}

Try it out

  1. Start the local web server by running the following command:

    npm run start:web
    

    Note

    Office Add-ins should use HTTPS, not HTTP, even when you are developing. If you are prompted to install a certificate after you run npm run start:web, accept the prompt to install the certificate that the Yeoman generator provides.

  2. In OneNote Online, open a notebook and create a new page.

  3. Choose Insert > Office Add-ins to open the Office Add-ins dialog.

    • If you're signed in with your consumer account, select the MY ADD-INS tab, and then choose Upload My Add-in.

    • If you're signed in with your work or school account, select the MY ORGANIZATION tab, and then select Upload My Add-in.

    The following image shows the MY ADD-INS tab for consumer notebooks.

    The Office Add-ins dialog showing the MY ADD-INS tab
  4. In the Upload Add-in dialog, browse to manifest.xml in your project folder, and then choose Upload.

  5. From the Home tab, choose the Show Taskpane button in the ribbon. The add-in task pane opens in an iFrame next to the OneNote page.

  6. At the bottom of the task pane, choose the Run link to set the page title and add an outline to the body of the page.

    The OneNote add-in built from this walkthrough

Next steps

Congratulations, you've successfully created a OneNote task pane add-in! Next, learn more about the core concepts of building OneNote add-ins.

See also