In a recent Sitecore project by Oshyn, I was given the task of creating main navigation and left categories navigation. I had to decide whether to build those navigations using either rendering or using a .NET user control.
The most common approach to building Sitecore navigation is rendering, because of its performance and easy-to-build and deploy code. A sublayout using a user .NET control is also a good alternative; it is becoming more common in recent Sitecore versions to see navigations implemented with sublayouts. The benefit to use a sublayout is the ability to use c# code to manipulate nodes and its properties.
The navigation menu I needed to develop was very straightforward and since I had the chance to start creating the nodes myself, I chose to build the main navigation and left navigation using rendering.
The structure of the site is similar to this graph:
Include in navigation (IncludeInNav),is channel page (IsChannelPage) and include in sitemap (IncludeInSitemap) are the properties that each page node will need to have. The first property tells if a page node is eligible for the top navigation. The second property tells if a page node is the channel page (a channel page is the main page in a category hierarchy), and the third option is used for creating a sitemap (I will talk about creating a sitemap in a future post).
On the rendering code, we need to set a home variable. This home variable represents the root node or home node. Starting from the home variable, the code will extract all child nodes that meet the IncludeInNav property and the IsChannelPage to true. Now that we have a set of nodes that are channel pages (categories), we proceed to iterate on every one and print the appropriate HTML for that node. For each category, we also execute a query to find the children of each category.
To do this on every iteration of the categories nodes, we query again down the subtree for nodes that meet the IncludeInNav parameter to true. For each of the retrieved set we print the appropriate HTML.
As you can see, creating the top navigation structure using rendering is a very straightforward approach. It loads very fast, which I was very please with.
Here are some important lines of code that you might find useful.
Selecting home node:
Iterating through categories nodes: