/// <summary> /// Creates a new content writer for a particular page node in the OneNote hierarchy. Content /// will not be committed to the page until close. /// </summary> /// <param name="node">A page node in the onenote hierarchy.</param> public ContentWriter(XmlNode node) { _pageNode = node; string pageXml; _application.GetPageContent(_pageNode.Attributes["ID"].Value, out pageXml, PageInfo.piBasic); onXml = new OneNoteXml( ); onXml.Document.LoadXml(pageXml); }
/// <summary> /// Creates a new OneNote "thing" -- notebook, section, section group, or page. /// </summary> /// <param name="path">The path of the new thing to create.</param> /// <param name="itemTypeName">The type of thing to create.</param> /// <param name="newItemValue">In the notebook creation case, this parameter is the directory in which /// the notebook directory will get created.</param> protected override void NewItem(string path, string itemTypeName, object newItemValue) { WriteDebug("In NewItem. Path = " + path); string parentPath = SessionState.Path.ParseParent(path, null); string childName = SessionState.Path.ParseChildName(path); WriteDebug("Parsed path as: Parent = " + parentPath + ", child = " + childName); XmlNode parent = getOneNoteNode(parentPath); if (parent == null) { WriteInvalidPathError(parentPath); return; } // // Find out what we're creating. Per guidelines, itemTypeName is case-insensitive, // and it's sufficient for it to be enough characters to disambiguate between the options // in OneNoteTypes. // List <string> candidates = new List <string>( ); Microsoft.Office.OneNote.PowerShell.Utilities.GetCandidatesForString(OneNoteTypes, itemTypeName, candidates); if (candidates.Count > 1) { WriteError(new ErrorRecord( new ArgumentException("itemTypeName is ambiguous"), "InvalidArgument", ErrorCategory.InvalidArgument, itemTypeName) ); return; } if (candidates.Count == 0) { WriteError(new ErrorRecord( new ArgumentException("itemTypeName not a recognized OneNote type"), "InvalidArgument", ErrorCategory.InvalidArgument, itemTypeName) ); return; } string selectedType = candidates[0]; Microsoft.Office.Interop.OneNote.ApplicationClass app = new ApplicationClass( ); string id; switch (selectedType) { case "Notebook": string notebookPath = SessionState.Path.Combine((string)newItemValue, childName); WriteVerbose("Creating notebook in " + notebookPath); app.OpenHierarchy(notebookPath, "", out id, CreateFileType.cftNotebook); break; case "Directory": case "Section": if (parent.LocalName != "Notebook") { WriteError(new ErrorRecord(new ArgumentException(path + " is not a valid section path: It is not contained in a notebook."), "InvalidArgument", ErrorCategory.InvalidArgument, path)); return; } WriteVerbose("Creating section " + childName + " in notebook ID " + parent.Attributes["ID"].Value); app.OpenHierarchy(childName + ".one", parent.Attributes["ID"].Value, out id, CreateFileType.cftSection); break; case "Page": // // Creating pages is a bit more work than creating notebooks or sections. I need to build // up a bit of XML describing the page creation operation. // WriteVerbose("Creating page " + childName); string parentId = parent.Attributes["ID"].Value; OneNoteXml command = new OneNoteXml( ); XmlElement section = command.CreateSection(null, parentId); command.Document.AppendChild(section); XmlElement page = command.CreatePage(childName, null); section.AppendChild(page); string xml = command.Document.OuterXml; WriteVerbose(xml); app.UpdateHierarchy(xml); break; } }
/// <summary> /// Creates a new OneNote "thing" -- notebook, section, section group, or page. /// </summary> /// <param name="path">The path of the new thing to create.</param> /// <param name="itemTypeName">The type of thing to create.</param> /// <param name="newItemValue">In the notebook creation case, this parameter is the directory in which /// the notebook directory will get created.</param> protected override void NewItem(string path, string itemTypeName, object newItemValue) { WriteDebug("In NewItem. Path = " + path); string parentPath = SessionState.Path.ParseParent(path, null); string childName = SessionState.Path.ParseChildName(path); WriteDebug("Parsed path as: Parent = " + parentPath + ", child = " + childName); XmlNode parent = getOneNoteNode(parentPath); if (parent == null) { WriteInvalidPathError(parentPath); return; } // // Find out what we're creating. Per guidelines, itemTypeName is case-insensitive, // and it's sufficient for it to be enough characters to disambiguate between the options // in OneNoteTypes. // List<string> candidates = new List<string>( ); Microsoft.Office.OneNote.PowerShell.Utilities.GetCandidatesForString(OneNoteTypes, itemTypeName, candidates); if (candidates.Count > 1) { WriteError(new ErrorRecord( new ArgumentException("itemTypeName is ambiguous"), "InvalidArgument", ErrorCategory.InvalidArgument, itemTypeName) ); return; } if (candidates.Count == 0) { WriteError(new ErrorRecord( new ArgumentException("itemTypeName not a recognized OneNote type"), "InvalidArgument", ErrorCategory.InvalidArgument, itemTypeName) ); return; } string selectedType = candidates[0]; Microsoft.Office.Interop.OneNote.ApplicationClass app = new ApplicationClass( ); string id; switch (selectedType) { case "Notebook": string notebookPath = SessionState.Path.Combine((string)newItemValue, childName); WriteVerbose("Creating notebook in " + notebookPath); app.OpenHierarchy(notebookPath, "", out id, CreateFileType.cftNotebook); break; case "Directory": case "Section": if (parent.LocalName != "Notebook") { WriteError(new ErrorRecord(new ArgumentException(path + " is not a valid section path: It is not contained in a notebook."), "InvalidArgument", ErrorCategory.InvalidArgument, path)); return; } WriteVerbose("Creating section " + childName + " in notebook ID " + parent.Attributes["ID"].Value); app.OpenHierarchy(childName + ".one", parent.Attributes["ID"].Value, out id, CreateFileType.cftSection); break; case "Page": // // Creating pages is a bit more work than creating notebooks or sections. I need to build // up a bit of XML describing the page creation operation. // WriteVerbose("Creating page " + childName); string parentId = parent.Attributes["ID"].Value; OneNoteXml command = new OneNoteXml( ); XmlElement section = command.CreateSection(null, parentId); command.Document.AppendChild(section); XmlElement page = command.CreatePage(childName, null); section.AppendChild(page); string xml = command.Document.OuterXml; WriteVerbose(xml); app.UpdateHierarchy(xml); break; } }