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));
                }
            }
        }
        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));
                }
            }
        }