0

Sitecore – managing children of items stored in a bucket

Item buckets are a wonderful feature in Sitecore. Since their (official) introduction they’ve opened up a lot of possibilities and allowed for much larger data storage inside Sitecore. Alas, for all their glory, item buckets do come with some annoyances/weird behaviours. One of those issues is the difficulty to mange the children of an item stored in a bucket.  To a degree I’m a little surprised there’s no easy way to manage the children given you have the option to lock the child relationship.

bucket_lock_children

The image below should be an a-typical example of viewing an item stored in a bucket. By default, when clicking an item from the search results, Sitecore opens the item in a tabbed content editor. The problem here is we have no visibility of the children. For starters the content tree is hidden, this is easily solved as we can just show the tree. Unfortunately it will just display exactly what you see in the “parent” content editor, no use at all.

bucket_item_children_issue

To me the desired result is simple, show the content tree and display the item with its children.

bucket_item_children_solution

Do I hear cries of “sorcery!” “It’s not possible!” And “I don’t believe it!” Well no I’m afraid it’s not sorcery, in fact it’s a simple bit of magic.

The key to getting the effect in the above image is to override the uiLaunchResult processor. This processor is responsible for launching the content editor tab. Inside the processor it builds up the URL used to open a content editor window.

<processors>
      <uiLaunchResult>
        <processor desc="sitecore"   type="Sitecore.Buckets.Pipelines.UI.LaunchResultPipeline.LaunchSitecoreResult, Sitecore.Buckets"/>
      </uiLaunchResult>
</processors>

We need to create our own class and inherit from the existing one. After this we’ll need override the LaunchResult method and make two changes. Unfortunately it is a classic Sitecore class in that you will have to copy a private method from the base class and the existing LaunchResult method. Given the number of lines in the existing method I’ve created a Gist with the full class and just highlighted the main changes below.

  1. Add the “ro” (root item) parameter with the item ID value to the URL if the item is contained within a bucket.
  2. Move the “mode” parameter line into an else statement to keep the existing functionality of hiding the content tree when an item is not in a bucket.
if (Sitecore.Buckets.Managers.BucketManager.IsItemContainedWithinBucket(item))
{
	urlString.Add("ro", itemId);
}
else
{
	urlString.Add(Sitecore.Buckets.Util.Constants.ModeQueryStringKeyName, "preview");
}

Finally we just need to patch our new processor in.

<processors>
	<uiLaunchResult>
		<processor desc="sitecore">
			<patch:attribute name="type">Rocketcore.Buckets.LaunchSitecoreResult, Rocketcore</patch:attribute>
		</processor>
	</uiLaunchResult>
</processors>

Jason Bert