Search code examples
c#sitecoresitecore7

Sitecore - Go back to parent bucket


I have a question regarding bucket items in Sitecore.

I have following structure:

enter image description here

I want to create a button on the 'test' detail page that goes back to the top bucket 'News overview'. Normally I would do something like:

LinkManager.GetItemUrl(Sitecore.Context.Item.Parent)

The problem here is that the direct parent is the bucket '44' and not 'News overview'. What would be the best way to create a link to the overview bucket?


Solution

  • There is an extension method in the Item that gives you the bucket item of your current item.

    Its in the Sitecore.Buckets.Extensions namespace in the Sitecore.Buckets.dll assembly.

    You can use it like this:

    var bucketItem = Sitecore.Context.Item.GetParentBucketItemOrParent();
    var urlToBucket = LinkManager.GetItemUrl(bucketItem);
    

    You can also use the BucketManager to check if an item is contained within a bucket:

    BucketManager.IsItemContainedWithinBucket(Sitecore.Context.Item)