Sitecore Item Buckets with Different Bucket Folder Path Strategies

Problem: 

If you have multiple item buckets in your solution, they all use the same bucket folder path in Sitecore.Buckets.config under the BucketConfiguration.DynamicBucketFolderPath setting, mainly DateBasedFolderPath. I wanted to be able to use a different strategy for each bucket.

FIX:

I created a DynamicBucketFolderResolver which implements IDynamicBucketFolderPath:

public class DynamicBucketFolderResolver : IDynamicBucketFolderPath
{
#region Methods

public string GetFolderPath(ID newItemId, ID parentItemId, DateTime creationDateOfNewItem)
{
Database db = Sitecore.Data.Database.GetDatabase(“master”);
Item parentItem = db.GetItem(parentItemId);
if (parentItem != null)
{
string assemblyAndClass = parentItem.GetFieldValue(“__Dynamic Bucket Folder Path Class”);
if (!string.IsNullOrEmpty(assemblyAndClass))
{
Type resolverType = Type.GetType(assemblyAndClass);
IDynamicBucketFolderPath type = Sitecore.Reflection.ReflectionUtil.CreateObject(resolverType) as IDynamicBucketFolderPath;
return type.GetFolderPath(newItemId, parentItemId, creationDateOfNewItem);
}
}
return new Sitecore.Buckets.Util.DateBasedFolderPath().GetFolderPath(newItemId, parentItemId, creationDateOfNewItem);
}

#endregion
}

In Sitecore.Buckets.config, I’ve set the BucketConfiguration.DynamicBucketFolderPath to:

<setting name=”BucketConfiguration.DynamicBucketFolderPath” value=”Site.Web.Code.Buckets.BucketFolderPath.DynamicBucketFolderResolver, Site.Web”/>

In Sitecore, I’ve inserted a template field titled “__Dynamic Bucket Folder Path Class” as a single line text field to /sitecore/templates/System/Templates/Sections/Item Buckets/Item Buckets.

How it works:

I can now specify a namespace.class, assembly in the Standard Field, Dynamic Bucket Folder Path Class, so I can use any custom bucketing strategy I want for that specific bucket.

Capture

Now this bucket will use the GuidFolderPath bucket strategy from Alex Shyba’s autohaus solution.

Thanks Alex Shyba for your help with this one.

2 thoughts on “Sitecore Item Buckets with Different Bucket Folder Path Strategies

Add yours

    1. You wouldn’t be inserting items on a web database if you are using workflow and publishing. But you could if you are creating items programmatically, so if you are, you could change this code to: Database db = Sitecore.Data.Database.GetDatabase(“master”); if (db == null) { db = Sitecore.Data.Database.GetDatabase(“web”); }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: