Sitecore - Go back to parent bucket
Asked Answered
H

2

6

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?

Hervey answered 24/3, 2014 at 12:45 Comment(0)
G
12

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)
Groningen answered 24/3, 2014 at 15:43 Comment(0)
P
1

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.

Photomural answered 24/3, 2014 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.