Monday, May 25, 2009

Custom breadcrumb in sharepoint 2007

Hi every one,

In my recent sharepoint project,I need to use the breadcrumbs but the default breadcrumb provided in sharepoint is less flexible. For example if your pages are in 'Pages' directory then in the bradcrumb, there will be a link for 'pages' directory also which in most of the cases we dont want end user to view. The SiteMapPath control from ASP.net can be used to create a custom breadcrumb but I used an easy approach(as i felt) for it. I used C# and write some code as below-


try
{
String[] nav = null;
string[] navUrl = null;
nav = new string[10];
navUrl = new string[10];
int i = 0;
SPWeb web = SPControl.GetContextWeb(Context);
if (web.IsRootWeb == true)
{
nav[0] = web.Title;
navUrl[0] = web.Url.ToString() + "/" + SPContext.Current.File.Url.ToString();
}
else
{
while (!web.IsRootWeb)
{
nav[i] = web.Title;
navUrl[i] = web.Url.ToString() + "/Pages/Default.aspx";
web.Title = web.ParentWeb.Title;
web = web.ParentWeb;
i++;

}
nav[i] = web.Title;
navUrl[i] = web.Url.ToString() + "/" + SPContext.Current.File.Url.ToString();
}
string navigationStr = string.Empty;
navigationStr = "
Home";//set the home link

while (i > 0)
{
navigationStr = navigationStr + ">" + "
" + nav[i - 1] + "";
i--;
}
navigationStr = navigationStr + ">" + SPContext.Current.File.Title;
Table tblMain = new Table();
mainDiv.Controls.Add(tblMain);// mainDiv is a DIV tag works as container for the table
TableRow trMain = new TableRow();
tblMain.Rows.Add(trMain);
TableCell tdBrad = new TableCell();
trMain.Cells.Add(tdBrad);
tdBrad.Text = navigationStr;
}
catch
{
}


thats it. I put this code in an user control and deployed that control using smartpart(a third party tool). we may also create a class library project to create webpart for breadcrumb.

Hopes It will be helpful for needy people. :)

Regards

Deewaker