Creating dynamic menus in EPiServer

Wednesday, February 10th, 2010 | .NET

My first post in almost a year! What is going on! "Been busy, things are happening, blah blah blah"…no excuse, really. I signed up for twitter almost a year ago, and I think I lost some focus in the blogging sphere. Anyway, this first post in 2010 is about EPiServer, the best CMS out there today!

The challenge

I want to create a more dynamic page tree than the standard EPiServer PageTree. More like an Accordion panel which has 2 or 3 levels (doesn't matter) and all available nodes are expanded by default. The real challenge is to get the whole page tree with all nodes expanded (not only the nodes which are selected) and setting correct class on the expanded items. Whether this is an accordion or another cool menu script is all the same.

The Solution

Start out with the default PageTree control in EPiServer controls.

<EPiServer:PageTree ID="PageTreeLeftMenu" runat="server" ExpandAll="true" NumberOfLevels="2">

<HeaderTemplate>

<div class="leftmenu"> <!– Header –>

</HeaderTemplate>

<IndentTemplate>

<ul> <!– Indent –>

</IndentTemplate>

<ItemHeaderTemplate>

<li <%# GetExpandedClass(Container.DataItem) %>>

</ItemHeaderTemplate>

<ExpandedTopTemplate>

<EPiServer:Property PropertyName="PageLink" runat="server" /> <!– ExpandedTop –>

</ExpandedTopTemplate>

<SelectedExpandedTopTemplate>

<EPiServer:Property PropertyName="PageLink" runat="server" CssClass="selected" /> <!– SelectedExpandedTop –>

</SelectedExpandedTopTemplate>

<ExpandedItemTemplate>

<EPiServer:Property PropertyName="PageLink" runat="server" /> <!– ExpandedItem –>

</ExpandedItemTemplate>

<SelectedExpandedItemTemplate>

<EPiServer:Property PropertyName="PageLink" runat="server" CssClass="selected" /> <!– SelectedExpandedItem –>

</SelectedExpandedItemTemplate>

<ItemFooterTemplate>

</li>

</ItemFooterTemplate>

<UnindentTemplate>

</ul> <!– UnIndent –>

</UnindentTemplate>

<FooterTemplate>

</div> <!– Footer –>

</FooterTemplate>

</EPiServer:PageTree>


Things to notice:

  • "ExpandAll" is set to true. This will dump the whole tree, not just the nodes which are selected by the user.
  • Only the *Expanded* templates are in use for Top and Item (ie.ExpandedTopTemplate).  When using "ExpandAll" these are the templates used, not the regular Top Template and ItemTemplate.
  • <%# GetExpandedClass(Container.DataItem) %> – This is the magic for setting the correct style on the expanded items. In addition there is a "selected" class on the selected menu item

The Codebehind looks like this:

protected void Page_Load(object sender, EventArgs e)

{

PageTreeLeftMenu.PageLink = LeftMenuPageLink;

PageTreeLeftMenu.DataBind();

}

 

protected PageReference LeftMenuPageLink

{

get

{

PageReference leftMenuPageLink = CurrentPage["LeftMenuRoot"] as PageReference;

return leftMenuPageLink;

}

}

 

protected string GetExpandedClass(object dataItem)

{

PageData page = dataItem as PageData;

if (page != null && PageTreeLeftMenu.OpenPages.Contains(page.PageLink))

return "class=\"expanded\"";

return string.Empty;

 

}

Things to notice:

  • LeftmenuPageLink is just an example on how to set the start point for the left menu.
  • GetExpandedClass is the method that sets the correct class on the <li>. This is done by checking if the page in the tree is available in PageTree.OpenPages. This is what makes it possible to make the dynamic menu. If the OpenPages method was not available, it would not be possible to highlight the expanded nodes without traversing each node or doing some javascripting.

The output should look something like this

43 <div class="leftcolumn">

44 <div class="leftmenu">

45 <!– Header –>

46 <ul>

47 <!– Indent –>

48 <li class="expanded"><a href="/en/Consulting/">Consulting</a>

49 <!– ExpandedTop –>

50 <ul>

51 <!– Indent –>

52 <li class="expanded"><a class="selected" href="/en/Consulting/Business-Consulting-/">

53 Business Consulting </a>

54 <!– SelectedExpandedItem –>

55 </li>

56 <li><a href="/en/Consulting/Project-and-Test-Management/">Project and Test

57 Management</a>

58 <!– ExpandedItem –>

59 </li>

60 <li><a href="/en/Consulting/IT-Consulting/">IT Consulting</a>

61 <!– ExpandedItem –>

62 </li>

63 <li><a href="/en/Consulting/Application-Development/">Application Development</a>

64 <!– ExpandedItem –>

65 </li>

66 </ul>

67 <!– UnIndent –>

68 </li>

69 <li><a href="/en/Solutions/">Solutions</a>

70 <!– ExpandedTop –>

71 <ul>

72 <!– Indent –>

73 <li><a href="/en/Solutions/ERP/">ERP</a>

74 <!– ExpandedItem –>

75 </li>

76 <li><a href="/en/Solutions/Content-Management/">Content Management</a>

77 <!– ExpandedItem –>

78 </li>

79 <li><a href="/en/Solutions/Oil-and-Gas/">Oil and Gas</a>

80 <!– ExpandedItem –>

81 </li>

82 <li><a href="/en/Solutions/Industry/">Industry</a>

83 <!– ExpandedItem –>

84 </li>

85 <li><a href="/en/Solutions/E-Business/">E-Business</a>

86 <!– ExpandedItem –>

87 </li>

88 <li><a href="/en/Solutions/Banking–Finance-Suite/">Banking &amp; Finance

89 Suite</a>

90 <!– ExpandedItem –>

91 </li>

92 <li><a href="/en/Solutions/Public-Sector-Suite/">Public Sector Suite</a>

93 <!– ExpandedItem –>

94 </li>

95 </ul>

96 <!– UnIndent –>

97 </li>

98 <li><a href="/en/Outsourcing/">Outsourcing</a>

99 <!– ExpandedTop –>

100 <ul>

101 <!– Indent –>

102 <li><a href="/en/Outsourcing/ITO/">ITO</a>

103 <!– ExpandedItem –>

104 </li>

105 <li><a href="/en/Outsourcing/Applications-Management/">Applications Management</a>

106 <!– ExpandedItem –>

107 </li>

108 </ul>

109 <!– UnIndent –>

110 </li>

111 <li><a href="/en/TestTest/">TestTest</a>

112 <!– ExpandedTop –>

113 </li>

114 <li><a href="/en/TestTestTest/">TestTestTest</a>

115 <!– ExpandedTop –>

116 </li>

117 </ul>

118 <!– UnIndent –>

119 </div>

120 <!– Footer –>

121 </div>

The result will look something like this:

After adding the Accordion script, the Top items should collapse and only the expanded section should be visible. When moving the mouse over another section it should expand, but not highlight. The final result will look something like this.

Conclusion

Using dynamic menus could give the visitor a better experience. Whether its an Accordion panel or some other fancy scripted menu doesn't matter. Anyway you will have to dump the whole tree to a certain level (2 levels in this example) and set the correct classes on the elements to get the styling correct. The magic is the "PageTree.OpenPages" method which helps a lot in this scenario.

1 Comment to Creating dynamic menus in EPiServer

Øyvind
February 11, 2010

Good to have you back blogging.

Leave a comment