none
oneNote 2013 programing c# with visual studio 2015 RRS feed

  • Question

  • hi, I'm trying to write a script that will open a new onenote 2013 page each time and past images to that page.

    I'm using visual studio 2015. Can you please help me with the library's that I need to add in order for it to work. 

    this is the code i'm using:

    static

    Application onenoteApp = newApplication();

               

    staticXNamespacens = null;

               

    staticvoidMain(string[] args)

                {

                    GetNamespace();

                   

    stringnotebookId = GetObjectId(null, HierarchyScope.hsNotebooks, "Tasks");

                   

    stringsectionId = GetObjectId(notebookId, HierarchyScope.hsSections, "Notes");

                   

    stringpageId = CreatePage(sectionId, "Test");

                }

               

    staticvoidGetNamespace()

                {

                   

    stringxml;

                    onenoteApp.GetHierarchy(

    null, HierarchyScope.hsNotebooks, outxml);

                   

    vardoc = XDocument.Parse(xml);

                    ns = doc.Root.Name.Namespace;

                }

               

    staticstringGetObjectId(stringparentId, HierarchyScope scope, stringobjectName)

                {

                   

    stringxml;

                    onenoteApp.GetHierarchy(parentId, scope,

    outxml);

                   

    vardoc = XDocument.Parse(xml);

                   

    varnodeName = "";

                   

    switch(scope)

                    {

                       

    case(HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;

                       

    case(HierarchyScope.hsPages): nodeName = "Page"; break;

                       

    case(HierarchyScope.hsSections): nodeName = "Section"; break;

                       

    default:

                           

    returnnull;

                    }

                   

    varnode = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();

                   

    returnnode.Attribute("ID").Value;

                }

               

    staticstringCreatePage(stringsectionId, stringpageName)

                {

                   

    // Create the new page


                   

    stringpageId;

                    onenoteApp.CreateNewPage(sectionId,

    outpageId, NewPageStyle.npsBlankPageWithTitle);

                   

    // Get the title and set it to our page name


                   

    stringxml;

                    onenoteApp.GetPageContent(pageId,

    outxml, PageInfo.piAll);

                   

    vardoc = XDocument.Parse(xml);

                   

    vartitle = doc.Descendants(ns + "T").First();

                    title.Value = pageName;

                   

    // Update the page


                    onenoteApp.UpdatePageContent(doc.ToString());

                   

    returnpageId;

                }

            }

    Thursday, November 10, 2016 9:50 AM

All replies

  • Hi sudelman,

    Based on your code, you have achieved creating a new page.

    For inserting images, I suggest you try below methods.

            public void InsertImgById(string pageId)
            {
                string strNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
                string m_xmlImageContent =
    "<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
                string m_xmlNewOutline =
                   "<?xml version=\"1.0\"?><one:Page xmlns:one=\"{2}\" ID=\"{1}\"><one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>{0}</one:Page>";
    
    
                Bitmap bitmap = new Bitmap(@"C:\Users\your image path\test.png");
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Jpeg);
                string fileString = Convert.ToBase64String(stream.ToArray());
                string notebookXml;           
    
                onenoteApp.GetHierarchy(null, HierarchyScope.hsPages, out notebookXml);
                var doc = XDocument.Parse(notebookXml);
               // var ns = doc.Root.Name.Namespace;
    
                var existingPageId = pageId;
                var pageNodeName = doc.Descendants(ns + "Page").Where(n => n.Attribute("ID").Value == existingPageId).FirstOrDefault();
                string pageToBeChange = pageNodeName.Attribute("name").Value;
    
                string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
                //string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageToBeChange });
                string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, ns, pageToBeChange });
                onenoteApp.UpdatePageContent(pageChangesXml.ToString(), DateTime.MinValue);
            }
    //call InsertImgById
                    GetNamespace();
                    string notebookId = GetObjectId(null, HierarchyScope.hsNotebooks, "your Notebook");
                    string sectionId = GetObjectId(notebookId, HierarchyScope.hsSections, "your section");            
                    string pageId = CreatePage(sectionId, "Test");
                    InsertImgById(pageId);

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 11, 2016 8:02 AM