OneNoteConversionTool.OutputGenerator.OneNoteGenerator.CreateSection C# (CSharp) Method

CreateSection() public method

Creates one note section under the given notebook ID
public CreateSection ( string sectionName, string notebookId ) : string
sectionName string
notebookId string
return string
		public string CreateSection(string sectionName, string notebookId)
		{
			string sectionId;

			try
			{
				//read notebook xml to get all the existed section names
				string docXml;
				_mApp.GetHierarchy(notebookId, HierarchyScope.hsSections, out docXml);
				var xDoc = XDocument.Parse(docXml);
				XNamespace xNs = xDoc.Root.Name.Namespace;
				var sectionNames = xDoc.Root.Elements(xNs + "Section").Select(section => section.Attribute("name").Value);

				//if sectionName already exist, set sectionName as sectionName (2), and etc..
				int ordinal = 2;
				var adjustedSectionName = sectionName;
				while (sectionNames.Contains(adjustedSectionName))
				{
					adjustedSectionName = sectionName + " (" + ordinal + ")";
					++ordinal;
				}

				_mApp.OpenHierarchy(adjustedSectionName + ".one", notebookId, out sectionId, CreateFileType.cftSection);
			}
			catch (Exception e)
			{
				throw new ApplicationException("Error in CreateSection: " + e.Message, e);
			}
			return sectionId;
		}

Usage Example

		public static void InitializeTestNotebook(TestContext testContext)
		{
			_mXmlNs = Utility.NS;

			_mTestNotebookDir = Path.GetTempPath();

			_mOneNoteGenerator = new OneNoteGenerator(_mTestNotebookDir);
			_mTestNotebookId = _mOneNoteGenerator.CreateNotebook(TestNotebookName);
			_mTestSectionId = _mOneNoteGenerator.CreateSection(TestSectionName, _mTestNotebookId);
			//for ValidateCreateSectionNameConflicts()
			_mOneNoteGenerator.CreateSection(TestSectionName, _mTestNotebookId);
			_mOneNoteGenerator.CreateSection(TestSectionName, _mTestNotebookId);

			_mTestPageId = _mOneNoteGenerator.CreatePage(TestPageName, _mTestSectionId);
			
			//This is ugly, but apparently creating notebook/section/page takes time
			Thread.Sleep(4000);
		}
All Usage Examples Of OneNoteConversionTool.OutputGenerator.OneNoteGenerator::CreateSection