/// <summary>
 /// Internal routine to copy from a source OneNote node to a destination OneNote node.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="dest"></param>
 private void copyItems(ApplicationClass _app, XmlNode source, XmlNode dest, bool recurse)
 {
     if (source.LocalName == "Page")
     {
         string sourceId = source.Attributes["ID"].Value;
         string destId   = dest.Attributes["ID"].Value;
         string newPageId;
         string pageContent;
         _app.GetPageContent(sourceId, out pageContent, PageInfo.piAll);
         _app.CreateNewPage(destId, out newPageId, NewPageStyle.npsDefault);
         XmlDocument page = new XmlDocument( );
         page.LoadXml(pageContent);
         page.DocumentElement.Attributes["ID"].Value = newPageId;
         WriteDebug("Just changed PageID to " + newPageId);
         XmlNodeList objectNodes = page.SelectNodes("//*[@objectID]");
         foreach (XmlNode objectNode in objectNodes)
         {
             XmlAttribute oid = objectNode.Attributes["objectID"];
             objectNode.Attributes.Remove(oid);
         }
         WriteDebug("Just removed object IDs from " + objectNodes.Count + " node(s)");
         string debugXml = Microsoft.Office.OneNote.PowerShell.Utilities.PrettyPrintXml(page);
         WriteDebug(debugXml.Substring(0, 256));
         _app.UpdatePageContent(page.OuterXml, DateTime.MinValue);
     }
     if (recurse)
     {
         throw new NotImplementedException( );
     }
 }
        protected override void ProcessRecord( )
        {
            if (_scope == HierarchyScope.hsSelf)
            {
                //
                //  Nothing to do in this case.
                //

                WriteVerbose("No TOC can be created for the hsSelf scope.");
                return;
            }
            //
            //  Determine if we're supposed to override the section ID in which we create the TOC.
            //

            string overrideSectionId = null;

            if (!String.IsNullOrEmpty(_tocSectionPath))
            {
                List <string> tocSections = new List <string>( );
                Utilities.GetOneNoteIdsForPsPath(this, _tocSectionPath, tocSections);
                if (tocSections.Count >= 1)
                {
                    overrideSectionId = tocSections[0];
                    WriteVerbose("New TOC pages will go in Section " + overrideSectionId);
                }
            }
            List <string> ids = new List <string>( );

            Utilities.GetOneNoteIdsForPsPath(this, path, ids);
            foreach (string id in ids)
            {
                string hierarchy;
                _application.GetHierarchy(id, HierarchyScope.hsPages, out hierarchy);
                XmlDocument hierarchyDoc = new XmlDocument( );
                hierarchyDoc.LoadXml(hierarchy);
                nsmgr = new XmlNamespaceManager(hierarchyDoc.NameTable);
                nsmgr.AddNamespace("one", Microsoft.Office.OneNote.PowerShell.Provider.OneNoteXml.OneNoteSchema);

                //
                //  We must have a Notebook, Section, or SectionGroup.
                //

                WriteDebug("Found " + hierarchyDoc.DocumentElement.LocalName);
                if (!_validNodeTypes.Contains(hierarchyDoc.DocumentElement.LocalName))
                {
                    string errorMessage = "You must specify the path to a Notebook, Section, or Section Group. '{0}' is a {1}";
                    WriteError(new ErrorRecord(new ArgumentException(String.Format(errorMessage, id, hierarchyDoc.DocumentElement.LocalName)),
                                               "ArgumentException",
                                               ErrorCategory.InvalidArgument,
                                               id)
                               );
                    continue;
                }
                if (hierarchyDoc.DocumentElement.ChildNodes.Count == 0)
                {
                    WriteError(new ErrorRecord(new ArgumentException("No sections exist to put into a TOC."),
                                               "ArgumentException",
                                               ErrorCategory.InvalidArgument,
                                               id)
                               );
                    continue;
                }

                //
                //  Build up the TOC as HTML, then build a OneNote XML envelope around it.
                //

                System.Text.StringBuilder tocHtml = new StringBuilder( );
                tocHtml.Append("<html><head></head><body><ul>\n");
                foreach (XmlElement e in hierarchyDoc.DocumentElement.ChildNodes)
                {
                    addElementHtml(e, tocHtml, "  ");
                }
                tocHtml.Append("</ul></body></html>\n");

                string sectionId;
                if (!string.IsNullOrEmpty(overrideSectionId))
                {
                    sectionId = overrideSectionId;
                }
                else
                {
                    sectionId = id;
                }
                Microsoft.Office.OneNote.PowerShell.Provider.OneNoteXml pageXml = new Microsoft.Office.OneNote.PowerShell.Provider.OneNoteXml();
                XmlElement pageElement = pageXml.CreatePage("Table of Contents", null);
                pageXml.Document.AppendChild(pageElement);
                XmlElement htmlData = pageXml.FindOrCreate(pageElement,
                                                           new string[] { "Outline", "OEChildren", "HTMLBlock", "Data" });
                htmlData.AppendChild(pageXml.Document.CreateCDataSection(tocHtml.ToString( )));

                if (ShouldProcess("Add TOC page?"))
                {
                    try
                    {
                        string newPageId;
                        _application.CreateNewPage(sectionId, out newPageId, NewPageStyle.npsBlankPageWithTitle);
                        XmlAttribute idAttribute = pageXml.Document.CreateAttribute("ID");
                        idAttribute.Value = newPageId;
                        pageElement.Attributes.Append(idAttribute);
                        _application.UpdatePageContent(Utilities.PrettyPrintXml(pageXml.Document), DateTime.MinValue);
                    } catch (Exception e)
                    {
                        WriteError(new ErrorRecord(e, "PageImport", ErrorCategory.WriteError, pageXml.Document));
                    }
                }
                if (_passThru)
                {
                    WriteObject(Utilities.PrettyPrintXml(pageXml.Document));
                }
            }
        }
 /// <summary>
 /// Internal routine to copy from a source OneNote node to a destination OneNote node.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="dest"></param>
 private void copyItems(ApplicationClass _app, XmlNode source, XmlNode dest, bool recurse)
 {
     if (source.LocalName == "Page")
     {
         string sourceId = source.Attributes["ID"].Value;
         string destId = dest.Attributes["ID"].Value;
         string newPageId;
         string pageContent;
         _app.GetPageContent(sourceId, out pageContent, PageInfo.piAll);
         _app.CreateNewPage(destId, out newPageId, NewPageStyle.npsDefault);
         XmlDocument page = new XmlDocument( );
         page.LoadXml(pageContent);
         page.DocumentElement.Attributes["ID"].Value = newPageId;
         WriteDebug("Just changed PageID to " + newPageId);
         XmlNodeList objectNodes = page.SelectNodes("//*[@objectID]");
         foreach (XmlNode objectNode in objectNodes)
         {
             XmlAttribute oid = objectNode.Attributes["objectID"];
             objectNode.Attributes.Remove(oid);
         }
         WriteDebug("Just removed object IDs from " + objectNodes.Count + " node(s)");
         string debugXml = Microsoft.Office.OneNote.PowerShell.Utilities.PrettyPrintXml(page);
         WriteDebug(debugXml.Substring(0, 256));
         _app.UpdatePageContent(page.OuterXml, DateTime.MinValue);
     }
     if (recurse)
     {
         throw new NotImplementedException( );
     }
 }