Custom Breadcrumbs In Kentico

In most cases, the Breadcrumbs web part in Kentico works quite well as a way to help users navigate their back up the content tree. However, for a project I was working on recently the content tree was not the proper hierarchy. In order to preserve some legacy content, the URLs were rewritten to appear to be hierarchical, but were actually placed elsewhere in the tree. In this case, the Breadcrumbs web part did not work, as it would display the content tree instead of the path based on the Document URL.

The solution I have created uses the PARSENAME built into SQL. <a href= target=”_blank” http://msdn.microsoft.com/en-us/library/ms188006.aspx”>The function is designed to be able to extract the parts of a database object name. I co-opted the function to extract the parent pages of the document by replacing the slash separator with a period.

This does have the limitation of only being able to handle URLs up to 4 levels deep. This was not an issue for the project I was working on, but it is a hard limitation on the PARSENAME function. In order to support an arbitrary number of levels a new approach would be needed.

DECLARE @BreadcrumbsForThisUrl varchar(max)
SET @BreadcrumbsForThisUrl = '/Level1/Level2/Level3'

DECLARE @BreadcrumbsForThisUrlWithoutLeadingSlash varchar(max)
SET @BreadcrumbsForThisUrlWithoutLeadingSlash = SUBSTRING(@BreadcrumbsForThisUrl, 2, LEN(@BreadcrumbsForThisUrl))

DECLARE @ThisUrlDepth int
SET @ThisUrlDepth = LEN(@BreadcrumbsForThisUrl) - LEN(REPLACE(@BreadcrumbsForThisUrl, '/', ''))

SELECT * 
FROM
(
    SELECT
    DocumentName
    , DocumentURLPath
    , LEN(DocumentUrlPath) - LEN(REPLACE(DocumentUrlPath, '/', '')) AS Depth 
    , 0 AS IsCurrentPage
    FROM View_CMS_Tree_Joined
    WHERE DocumentUrlPath LIKE '%' + REVERSE(PARSENAME(REPLACE(REVERSE(@BreadcrumbsForThisUrlWithoutLeadingSlash), '/', '.'), 1)) + '%'

) Pages
WHERE Depth < @ThisUrlDepth

UNION 

SELECT DocumentName, DocumentURLPath, @ThisUrlDepth AS Depth, 1 AS IsCurrentPage
FROM View_CMS_Tree_Joined
WHERE DocumentURLPath = @BreadcrumbsForThisUrl
ORDER BY Depth

The following simple transformation can be used to display the Breadcrumbs:

<%# (Eval("IsCurrentPage").ToString() == "0") ? "<a href=\"" + Eval("DocumentURLPath") + "\">" + Eval("DocumentName") + "</a> &raquo; " : Eval("DocumentName")  %>

Written on October 17th, 2014 by Dan Walker
Tags: kentico, sql.

Want to read more?

If you’ve enjoyed what you’ve seen so far, you might like some of my other posts. Visit the archive to see all of my past writings.

Blog Archive »
 

About the Author

Dan Walker is a programmer from Grand Rapids, MI. He works at Gordon Food Service.

More About Dan »

Get in touch