contact@skillcore.net

Sitecore Item Buckets - custom folders structure

Posted 3 December 2015 by Marek Musielak

sitecore item buckets - custom folder structure

Item Buckets allow you to store millions of items in Sitecore tree without worrying about the structure of the nested items. In theory, you should only use Item Buckets for the data which does not require any hierarchy. Also you should not have to browse the Sitecore content tree, trying to find some particular item in a bucket. Still, sometimes we need to find an item in the tree and it's not that easy when the only help we have is default Sitecore date-based folder structure of Item Buckets. Fortunately, Sitecore allows to change the structure of Item Bucket folders easily.

Default Sitecore Item Buckets folder structure

In clean Sitecore installation, folder structure of Item Buckets is determined by the web.config setting:

<setting name="BucketConfiguration.BucketFolderPath" value="yyyy\/MM\/dd\/HH\/mm"/>

It means that every time one adds an item to the bucket, a folder structure based on the current date is created including year, month, day, hour and minute. So the simplest change we can do is to edit this setting to remove /mm part or to add /ss. In first scenario Sitecore will not use minutes while creating folder structure. In the second, Sitecore will also include seconds in the bucket folder structure.

Other out of the box options for Sitecore Item Buckets folder structure

Sitecore allows you to use different ways for folder structure generation out of the box as well. You can do this using Sitecore Rules Engine. First you need to find /sitecore/system/Settings/Buckets/Item Buckets Settings item in Sitecore tree and set chosen conditions and actions in Rules for Resolving the Bucket Folder Path field, e.g. like this:

where the ID of the item bucket is equal to Bucket Root
create the folder structure based on the name of the new bucketable item with 6 levels

There are 3 out of the box actions:

create the folder structure based on the creation date of the new bucketable item in this format
create the folder structure based on the ID of the new bucketable item with this number of levels
create the folder structure based on the name of the new bucketable item with this number of levels

First of them works in a pretty much the same way like the default setting in Sitecore config. Second and third actions create folder structure with a single letter node at each level based either on item name or item id. So if we add an item called bucketable and if we use the third rule with 6 levels of folders, our structure will look like this:

sitecore item buckets - create the folder structure based on the name of the new bucketable item with this number of levels

Create custom Sitecore Item Buckets folder structure

Sitecore also allows you to create your own rules and actions. Sometimes we need to find the bucketed item in the tree. It would be more helpful if all items in a bucket were grouped in folders named after their authors (with standard date based folder structure under it). So if admin created an item, it would be in Bucket Root/admin/2015/12/03/22/14 folder and if sitecore/marek created an item, it would be in Bucket Root/marek/2015/12/03/22/15 folder. Code for this rule is really simple:

public class AuthorAndCreateDateBasedPath<T> : CreateDateBasedPath<T> where T : BucketingRuleContext
{
  public override void Apply(T ruleContext)
  {
    base.Apply(ruleContext);
    if (Sitecore.Context.Data != null && Sitecore.Context.Data.User != null)
    {
      ruleContext.ResolvedPath = 
        Sitecore.Context.Data.User.LocalName + "/" + ruleContext.ResolvedPath;
    }
  }
}

I overriden CreateDateBasedPath rule and after applying base action, I prefix the path with current user name. Then we create new Action under /sitecore/system/Settings/Rules/Definitions/Elements/Bucketing item and set Text and Title fields like that:

sitecore item buckets - create the folder structure based on the author of the new bucketable item and date

And as a result we have nice folder structure where every author can easily find their items:

sitecore item buckets - create the folder structure based on the author of the new bucketable item and date

In a result, 5 lines of code allowed us to change the way of Sitecore Item Buckets folder generation. New method can be really useful when users need to find their own items from time to time. And remember, this is just a simple example. With Sitecore Rules Engine you can achieve much more. Your imagination is the limit.