/// <summary> /// Retrieve the Section ID for the specified special location /// </summary> /// <param name="oneNoteApplication"></param> /// <param name="specialLocation">SpecialLocation</param> /// <returns>string with section ID</returns> private static string GetSectionId(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation) { if (oneNoteApplication == null) { return(null); } // ReSharper disable once RedundantAssignment var unfiledNotesPath = ""; oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath); // ReSharper disable once RedundantAssignment var notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (string.IsNullOrEmpty(notebookXml)) { return(null); } Log.Debug().WriteLine(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (var xmlReader = new XmlTextReader(reader)) { while (xmlReader.Read()) { if (!"one:Section".Equals(xmlReader.Name)) { continue; } var id = xmlReader.GetAttribute("ID"); var path = xmlReader.GetAttribute("path"); if (unfiledNotesPath.Equals(path)) { return(id); } } } } finally { reader?.Dispose(); } return(null); }
/// <summary> /// Get the captions of all the open word documents /// </summary> /// <returns></returns> public static List <OneNotePage> GetPages() { List <OneNotePage> pages = new List <OneNotePage>(); try { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { if (oneNoteApplication != null) { string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { LOG.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { reader = null; while (xmlReader.Read()) { if ("one:Page".Equals(xmlReader.Name)) { if ("true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"))) { OneNotePage page = new OneNotePage(); page.PageName = xmlReader.GetAttribute("name"); page.PageID = xmlReader.GetAttribute("ID"); pages.Add(page); // For debugging //string pageXml = ""; //oneNoteApplication.GetPageContent(page.PageID, out pageXml, PageInfo.piAll, XMLSchema.xs2010); //LOG.DebugFormat("Page XML: {0}", pageXml); } } } } } finally { if (reader != null) { reader.Dispose(); } } } } } } catch (Exception ex) { LOG.Warn("Problem retrieving onenote destinations, ignoring: ", ex); } return(pages); }
/// <summary> /// Retrieve the Section ID for the specified special location /// </summary> /// <param name="oneNoteApplication"></param> /// <param name="specialLocation">SpecialLocation</param> /// <returns>string with section ID</returns> private static string GetSectionID(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation) { if (oneNoteApplication == null) { return(null); } string unfiledNotesPath = ""; oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath); string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { LOG.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { while (xmlReader.Read()) { if ("one:Section".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); string path = xmlReader.GetAttribute("path"); if (unfiledNotesPath.Equals(path)) { return(id); } } } } } finally { if (reader != null) { reader.Dispose(); } } } return(null); }
/// <summary> /// Retrieve the Section ID for the specified special location /// </summary> /// <param name="oneNoteApplication"></param> /// <param name="specialLocation">SpecialLocation</param> /// <returns>string with section ID</returns> private static string GetSectionID(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation) { if(oneNoteApplication == null) { return null; } string unfiledNotesPath = ""; oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath); string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if(!string.IsNullOrEmpty(notebookXml)) { LOG.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using(XmlTextReader xmlReader = new XmlTextReader(reader)) { while(xmlReader.Read()) { if("one:Section".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); string path = xmlReader.GetAttribute("path"); if(unfiledNotesPath.Equals(path)) { return id; } } } } } finally { if(reader != null) { reader.Dispose(); } } } return null; }
/// <summary> /// Get the captions of all the open word documents /// </summary> /// <returns></returns> public static List <OneNotePage> GetPages() { List <OneNotePage> pages = new List <OneNotePage>(); try { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { if (oneNoteApplication != null) { // ReSharper disable once RedundantAssignment string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { Log.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { reader = null; OneNoteSection currentSection = null; OneNoteNotebook currentNotebook = null; while (xmlReader.Read()) { if ("one:Notebook".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID))) { currentNotebook = new OneNoteNotebook { ID = xmlReader.GetAttribute("ID"), Name = xmlReader.GetAttribute("name") }; } } if ("one:Section".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentSection == null || !id.Equals(currentSection.ID))) { currentSection = new OneNoteSection { ID = xmlReader.GetAttribute("ID"), Name = xmlReader.GetAttribute("name"), Parent = currentNotebook }; } } if ("one:Page".Equals(xmlReader.Name)) { // Skip deleted items if ("true".Equals(xmlReader.GetAttribute("isInRecycleBin"))) { continue; } OneNotePage page = new OneNotePage { Parent = currentSection, Name = xmlReader.GetAttribute("name"), ID = xmlReader.GetAttribute("ID") }; if (page.ID == null || page.Name == null) { continue; } page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed")); pages.Add(page); } } } } finally { reader?.Dispose(); } } } } } catch (COMException cEx) { if (cEx.ErrorCode == unchecked ((int)0x8002801D)) { Log.Warn("Wrong registry keys, to solve this remove the OneNote key as described here: http://microsoftmercenary.com/wp/outlook-excel-interop-calls-breaking-solved/"); } Log.Warn("Problem retrieving onenote destinations, ignoring: ", cEx); } catch (Exception ex) { Log.Warn("Problem retrieving onenote destinations, ignoring: ", ex); } pages.Sort(delegate(OneNotePage p1, OneNotePage p2) { if (p1.IsCurrentlyViewed || p2.IsCurrentlyViewed) { return(p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed)); } return(string.Compare(p1.DisplayName, p2.DisplayName, StringComparison.Ordinal)); }); return(pages); }