Implementing Breadcrumbs on Sitecore

2019-09-19 13:24发布

I just wanna ask if there is someone here that have already made a breadcrumbs in Sitecore. I'm currently doing a Sitecore 8 MVC project that needs to have a breadcrumbs.

Currently I saw this website http://blog.ryanbailey.co.nz/2015/05/breadcrumbs-for-pages-in-sitecore.html. But It doesn't work for me yet because I don't know what to reference.

I just need to know how to get every item in the path of my current page I can handle it already.

Thanks

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-19 13:51

Something like this should do it:

public ICollection<Item> GetBreadcrumbs(Item current, SiteContext site)
{
    Item homeItem = site.StartItem;

    List<Item> breadcrumbs = new List<Item>();

    while (current != null)
    {
        // You may want to add additional logic to opt in/out of 
        // the breadcrumb based on a template/field
        breadcrumbs.Add(current);

        if (current == homeItem)
            break;

        current = current.Parent;
    }

    breadcrumbs.Reverse();

    return breadcrumbs;
}

And then:

var breadcrumbs = GetBreadcrumbs(Context.Item, Context.Site);
查看更多
冷血范
3楼-- · 2019-09-19 13:59

You can take the current item and then take all the ancestors of it.

var ancestors = currentItem.Axes.GetAncestors().ToList();

Then you can get home item and filter the ancestors to remove sitecore and content nodes.

ancestors = ancestors.SkipWhile(i => i.ID != home.Id.ToID()).ToList();
查看更多
等我变得足够好
4楼-- · 2019-09-19 14:01
public void GetBreadcrumbs(Item ParentItem)
        {
            List<BredCrumbDetails> lstBreadCrumbs = new List<BredCrumbDetails>();
            string strcurrenttitle = ParentItem.Name;
            Item currentitem = ParentItem;
            int i = 0;
            while (currentitem != null)
            {
                var ItemTemplateid = currentitem.TemplateID.ToString();
                var FolderTemplateId = "{}";
                if (ItemTemplateid != FolderTemplateId) //here we are removing folders
                {
                    BredCrumbDetails bcDetails = new BredCrumbDetails();
                    if (i == 0)
                    {
                        bcDetails.BCPageLink = null;
                        bcDetails.Title = currentitem.Name;
                        bcDetails.IsVisible = true;
                        lstBreadCrumbs.Add(bcDetails);
                    }
                    else
                    {
                        bcDetails.BCPageLink = currentitem.Paths.FullPath;
                        bcDetails.Title = currentitem.Name;
                        bcDetails.IsVisible = true;
                        lstBreadCrumbs.Add(bcDetails);

                    }
                    i++;
                    if (currentitem.Name == ("Home"))
                    {

                        break;
                    }
                    currentitem = currentitem.Parent;
                }
                else
                {
                    i++;
                    currentitem = currentitem.Parent;
                }
            }

            lstBreadCrumbs.Reverse();
            rptCrumbs.DataSource = lstBreadCrumbs;
            rptCrumbs.DataBind();

        }
查看更多
登录 后发表回答