Sitecore APIs: Creating an upto N level left navigation
Creating an up to N level of Left Navigation using Sitecore APIs or Renderings had always been cumbersome task for me. As while making this i many a times get confused on which step i m right now.
But, Finally i m able to make a structure of that type of code snippet and i am sure now that at least i can get help from this rendering (if i forget the logic :p ).
Only that was the reason that i thought to share it with you guys; as i am sure there are many programmers like me in town.
/**
Declaration of required namespaces
System.Text: this namespace will be used for creating stringbuilder object
Sitecore.Data: this namespace will allow to create Database Object
Sitecore.Data.Item : this will allow Item Object
Sitecore.Configuration: this will allow us to create Factory Object
Sitecore.Collections: this will allow us to create ChildList Object in Code below
Sitecore.Data.Comparers; This will allow us to create ItemComparer Object in Code below
**/
using System;
using System.Text;
using Sitecore.Data;
using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Collections;
namespace Layouts.Generalphbody
{
///
/// Summary description for GeneralphbodySublayout
///
public partial class GeneralphbodySublayout : System.Web.UI.UserControl
{
/*
This Piece of code will create to variable objects in memory which will be further being used in Code.
StringBuilder strLeftNav will append data until it reaches to n Level and then this string will be assigned to Label or Literal for display purpose
Database dbMaster will hold database from which you are about to pull Data; that can be Master, Web or Core
isCurrent will hold current menu selected to attach specific class to enhance it.
*/
#region Variables
StringBuilder strLeftNav = new StringBuilder();
Database dbMaster = Factory.GetDatabase("master");
bool isCurrent = false;
#endregion
/*
In Page Load Event we are creating an Item Object which will refer to the base content node
and a function call which will render left navigation
*/
private void Page_Load(object sender, EventArgs e)
{
Item rootItem = dbMaster.GetItem("/sitecore/content/home");
if (rootItem != null)
{
RenderLeftNav(rootItem);
}
}
private void RenderLeftNav(Item rootItem)
{
ChildList clChildern1 = rootItem.Children;
RenderSubitems(clChildern1, 1);
}
private void RenderSubitems(ChildList clChildern1, int iLevel)
{
strLeftNav.Append("
ltrlLeftMenu.Text = strLeftNav.ToString();
}
private void RenderOtherMenu(Item child)
{
strLeftNav.Append("");
strLeftNav.Append(RenderLink(child, 1, isCurrent));
}
private void RenderCurrentMenu(Item child)
{
strLeftNav.Append(" ");
strLeftNav.Append(RenderLink(child, 1, isCurrent));
}
private string RenderLink(Item child, int p, bool isCurrent)
{
StringBuilder strHREF = new StringBuilder();
string url = child.Paths.GetFriendlyUrl();
//if (Level == 3 && IsSelected)
// strHREF.Append(" ");
//else
// strHREF.Append(" ");
strHREF.Append(" " +
child.DisplayName + " ");
// strHREF.Append(" ");
return strHREF.ToString();
}
}
}
But, Finally i m able to make a structure of that type of code snippet and i am sure now that at least i can get help from this rendering (if i forget the logic :p ).
Only that was the reason that i thought to share it with you guys; as i am sure there are many programmers like me in town.
/**
Declaration of required namespaces
System.Text: this namespace will be used for creating stringbuilder object
Sitecore.Data: this namespace will allow to create Database Object
Sitecore.Data.Item : this will allow Item Object
Sitecore.Configuration: this will allow us to create Factory Object
Sitecore.Collections: this will allow us to create ChildList Object in Code below
Sitecore.Data.Comparers; This will allow us to create ItemComparer Object in Code below
**/
using System;
using System.Text;
using Sitecore.Data;
using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Collections;
namespace Layouts.Generalphbody
{
///
/// Summary description for GeneralphbodySublayout
///
public partial class GeneralphbodySublayout : System.Web.UI.UserControl
{
/*
This Piece of code will create to variable objects in memory which will be further being used in Code.
StringBuilder strLeftNav will append data until it reaches to n Level and then this string will be assigned to Label or Literal for display purpose
Database dbMaster will hold database from which you are about to pull Data; that can be Master, Web or Core
isCurrent will hold current menu selected to attach specific class to enhance it.
*/
#region Variables
StringBuilder strLeftNav = new StringBuilder();
Database dbMaster = Factory.GetDatabase("master");
bool isCurrent = false;
#endregion
/*
In Page Load Event we are creating an Item Object which will refer to the base content node
and a function call which will render left navigation
*/
private void Page_Load(object sender, EventArgs e)
{
Item rootItem = dbMaster.GetItem("/sitecore/content/home");
if (rootItem != null)
{
RenderLeftNav(rootItem);
}
}
private void RenderLeftNav(Item rootItem)
{
ChildList clChildern1 = rootItem.Children;
RenderSubitems(clChildern1, 1);
}
private void RenderSubitems(ChildList clChildern1, int iLevel)
{
strLeftNav.Append("
- ");
foreach (Item child in clChildern1)
{
Item itmChild = dbMaster.GetItem(child.Paths.Path);
if (child.TemplateName.ToLower() == "product section")
{
Item current = Sitecore.Context.Item;
if (child.ID == current.ID || child.Paths.IsAncestorOf(current))
{
isCurrent = true;
RenderCurrentMenu(child);
ChildList clChildern2 = child.Children;
if (clChildern2.Count > 0)
{
RenderSubitems(clChildern2, 2);
}
}
else if (iLevel != 1)
{
isCurrent = false;
RenderOtherMenu(child);
}
}
}
strLeftNav.Append("
ltrlLeftMenu.Text = strLeftNav.ToString();
}
private void RenderOtherMenu(Item child)
{
strLeftNav.Append("
strLeftNav.Append(RenderLink(child, 1, isCurrent));
}
private void RenderCurrentMenu(Item child)
{
strLeftNav.Append("
strLeftNav.Append(RenderLink(child, 1, isCurrent));
}
private string RenderLink(Item child, int p, bool isCurrent)
{
StringBuilder strHREF = new StringBuilder();
string url = child.Paths.GetFriendlyUrl();
//if (Level == 3 && IsSelected)
// strHREF.Append("
//else
// strHREF.Append("
strHREF.Append(" " +
child.DisplayName + " ");
// strHREF.Append("
return strHREF.ToString();
}
}
}
Comments
Post a Comment