Looking at the code for the sort pages powertoy

There really is not a lot to this code. Ultimately, it reads the names of each page grouping into a list and alphabetizes that list. The only consideration to this simple routine is the page level.

pageLevel is a property accessible via the XML from our 2010 API.  It replaces the 2007 attribute for “isPageLevel.” The code below reads through the pages one at a time. If the page is a top level page, it gets added to a list of level 1 pages. If it is a subpage, it gets added to a different list composed of the subpages. The only trick here is that I needed to add the pageLevel value into the list item node for each page, and there was not an extra variable I could use. The quick and dirty solution I came up with just stashes that string value at the end of the ID field. If it is a level 1 page, I don't need to store anything. Here's the code snippet: 

            //temp var for subpage level
            string temp;

            //loop through all of the pages and populate the pages for the section
            for (int i = 0; i < PagessXmlNode.Count; i++)
                temp = "";
                    temp = PagessXmlNode[i].Attributes.GetNamedItem("pageLevel").Value;
                catch(Exception Exc)
                    showErrorDialog  ("Error determining page level.  If this is a OneNote 2007 format notebook, 
this is expected. Exiting.\n Error "+ Exc.ToString()); Environment.Exit(-99); } if (temp != "1") { //I have hit a subpage.... allPagesNode[allPagesNode.Count - 1].subpages.Add(new Page(PagessXmlNode[i].Attributes["name"].Value, PagessXmlNode[i].Attributes["ID"].Value + temp)); //so the page ID is now the pageID + a single digit at the end that is pageLevel } else { //normal page and keep going as normal allPagesNode.Add(new Page(PagessXmlNode[i].Attributes["name"].Value, PagessXmlNode[i].Attributes["ID"].Value)); } temp = null; }


Then when I build the XML for the section hierarchy, I retrieve the page level and add it back to the pageLevel attribute:


   1:              string inputXML = "<?xml version=\"1.0\"?>";
   2:              inputXML += "<one:Section ID=\"" + this.Id + "\" xmlns:one=\"" + sONNamespace + "\">\n";
   3:              foreach (Page p in this.Pages)
   4:              {
   5:                  inputXML += "\t<one:Page ID=\"" + p.Id + "\" />\n";
   6:                  if (p.subpages.Count > 0)
   7:                  {
   8:                      p.subpages.Sort();
   9:                      foreach (Page sp in p.subpages)
  10:                      {
  11:                          //retrieve the ID and pageLevel from the ID string
  12:                          inputXML += "\t<one:Page ID=\"" + sp.Id.Substring(0, sp.Id.Length-1) +
				 "\" pageLevel=\"" + sp.Id[sp.Id.Length-1].ToString() + "\"/>\n";
  13:                      }
  14:                  }
  15:              }
  16:              inputXML += "</one:Section>";


Line 12 is the line that does that. 

Since the lists are all separate based on page level, this all works in the end. I'm attaching the source file for the code below and bypassing all the setup files and stuff like that.

 This was a “down and dirty” solution – I just wanted to get this done since folks were asking for it.  You can look through the source file to see how the page group lists were constructed.

 Questions, comments, concerns and criticisms always welcome,


Comments (4)

  1. John says:

    Sorry about the code formatting.  Too many conflicting code snippet addins…

  2. Mark Opti says:

    Your power toy has a bug. It does sorting, but in the process messes up subpages levels.  I.e. it is sorting without regard to hierrarchy of suppages.  

  3. Mark Opti says:

    Your power toy has a bug. It does sorting, but in the process messes up subpages levels.  I.e. it is sorting without regard to hierrarchy of suppages.  

  4. JohnGuin says:

    Mark – was this a section upgraded from 2007?

Skip to main content