Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 581
Filip Huysmans Avatar asked Mar 24 '14 12:03

Filip Huysmans


2 Answers

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)
like image 165
Richard Seal Avatar answered Sep 30 '22 06:09

Richard Seal


You could recursively call [Sitecore.Data.Items.Item].Parent and perform a check each time until you get to an item with a template id that either is the template ID that you are looking for or is different from the bucket folders' template IDs.

Alternatively, you could also use [Sitecore.Data.Items.Item].Axes.GetAncestors(), but this will return you all of the ancestors of the Item in top-down order.

Parent/child relationships and methods and techniques for accessing one another did not actually change in Sitecore 7. As such, any techniques that you used in Sitecore 6 should still be usable in Sitecore 7.

like image 38
Zachary Kniebel Avatar answered Sep 30 '22 06:09

Zachary Kniebel