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