Update a OneNote page, using the developer API

0

I tried to update a page in OneNote with the Microsoft reference : http://msdn.microsoft.com/en-us/library/office/jj680118.aspx

Here's my problem. When i tried to update my page with the correct ID, it throwed me an error saying : Exception from HRESULT: 0x80042000.

Here is my code :

  static void UpdatePageContent()
  {
        ApplicationClass onApplication = new ApplicationClass();
        String strImportXML;

        strImportXML = @"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" +
           "   <one:Page xmlns:one="+"http://schemas.microsoft.com/office/onenote/12/2004/onenote\""+"" +
           "ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
           "      <one:PageSettings RTL=\"false\" color=\"automatic\">" +
           "         <one:PageSize>" +
           "            <one:Automatic/>" +
           "         </one:PageSize>" +
           "         <one:RuleLines visible=\"false\"/>" +
           "      </one:PageSettings>" +
           "      <one:Title style=\"font-family:Calibri;" +
           "           font-size:17.0pt\" lang=\"en-US\">" +
           "         <one:OE alignment=\"left\">" +
           "            <one:T>" +
           "               <![CDATA[My Sample Page]]>" +
           "            </one:T>" +
           "         </one:OE>" +
           "      </one:Title>" +
           "      <one:Outline >" +
           "         <one:Position x=\"120\" y=\"160\"/>" +
           "         <one:Size width=\"120\" height=\"15\"/>" +
           "         <one:OEChildren>" +
           "            <one:OE alignment=\"left\">" +
           "               <one:T>" +
           "                  <![CDATA[Sample Text]]>" +
           "               </one:T>" +
           "            </one:OE>" +
           "         </one:OEChildren>" +
           "      </one:Outline>" +
           "   </one:Page>";

        // Update page content
        try
        {
            onApplication.UpdatePageContent(strImportXML, System.DateTime.MinValue);
        }
        catch (COMException e)
        {
            Console.WriteLine("Error Message : " + e.Message);
        }
    }

I really don't know how to solve this.

c#
xml
onenote
asked on Stack Overflow Feb 13, 2013 by Sebastien • edited Apr 10, 2014 by roryok

2 Answers

1

Your XML is not OneNote friendly.

Here's a list of error codes: http://msdn.microsoft.com/en-us/library/office/jj680117.aspx

You can get rid of the first line, as @Sebastian has stated it's malformed anyway and my experience is that OneNote doesn't need it.

Also, remember that you don't need to send the entire page. You just need to send the page's objId and any updated objects. So one outline needs adding then this should also work:

"<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/12/2004/onenote\" +
       "ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
       "      <one:Outline >" +
       "         <one:Position x=\"120\" y=\"160\"/>" +
       "         <one:Size width=\"120\" height=\"15\"/>" +
       "         <one:OEChildren>" +
       "            <one:OE alignment=\"left\">" +
       "               <one:T>" +
       "                  <![CDATA[New Text]]>" +
       "               </one:T>" +
       "            </one:OE>" +
       "         </one:OEChildren>" +
       "      </one:Outline>";

Just this new outline will get added.

If you still get problems (it doesn't complain but the content doesn't update) then check the extra parameters for UpdatePageContent, certainly in the 2013 API one can send a last modified date to check and also there's a parameter to force a local over-write.

answered on Stack Overflow Nov 3, 2013 by Darren Beale • edited Nov 3, 2013 by Darren Beale
0

There are some issues in the strImportXML string, which causes the update to fail.

  • Adjust @"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" to "<?xml version=\"" + "1.0" + "\" encoding=\"" + "utf-16" + "\"?>"
  • Add an empty space before the page ID attribute ( + " " + "ID...
    instead of + "" + "ID)
  • Make sure that the page ID is found/ present in your onApplication hierarchy
  • Make sure that you reference the COM library with the matching
    namespace defined in the one:Page element (e.g. Office 2013/ 15.0
    Object Library has another namespace)
answered on Stack Overflow Mar 8, 2013 by Dani

User contributions licensed under CC BY-SA 3.0