InvalidArgument Error when using copyToNotebook() in OneNote JavaScript API

松下 実樹 0 Reputation points
2025-04-24T12:37:01.6366667+00:00

I usually create OneNote APIs while looking at this site (https://learn.microsoft.com/ja-jp/javascript/api/onenote?view=onenote-js-1.1), but I'm having trouble with copyToNotebook(). I am having trouble with copyToNotebook().

It says that the argument is wrong, but I put a variable of type OneNote.Notebook as specified.

I can't do another copyToSection(destinationSection) either.

I get the same thing with the wrong argument, but it should be there.

Both use the exact same code as the sample code in the official reference.

What are some possible things that could be happening?

If not, what other code should I use to get the same behavior?

Thank you in advance.


Code


async function deleteInfo() {
  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();

    try {
      const newSection = section.copyToNotebook(notebook);
      newSection.load('id');
      await context.sync();
      console.log(newSection.id);
    } catch (copyError) {
      console.error(copyError);
      if (copyError.debugInfo) {
        console.error("Debug Info:", JSON.stringify(copyError.debugInfo, null, 2));
      }
      if (copyError.innerError) {
        console.error("Inner Error:", JSON.stringify(copyError.innerError, null, 2));
      }
    }
  });  
}

Error

Debug Info: {
  "code": "InvalidArgument",
  "message": "InvalidArgument",
  "errorLocation": "Section.copyToNotebook",
  "statement": "var copyToNotebook = v.copyToNotebook(...);",
  "surroundingStatements": [
    "var v = context.root._getObjectByReferenceId(\"{70f32172-415d-4679-abad-71d6bbbacf0b}{13}\");",
    "var v1 = context.root._getObjectByReferenceId(\"{70f32172-415d-4679-abad-71d6bbbacf0b}{12}\");",
    "// >>>>>",
    "var copyToNotebook = v.copyToNotebook(...);",
    "// <<<<<",
    "// Instantiate {copyToNotebook}",
    "copyToNotebook.load([\"id\",\"name\"]);"
  ],
  "fullStatements": [
    "Please enable config.extendedErrorLogging to see full statements."
  ]

OneNote Management
OneNote Management
OneNote: A family of Microsoft products that enable users to capture, organize, and reuse notes electronically.Management: The act or process of organizing, handling, directing or controlling something.
195 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.