Site icon Horizontal Blog

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.

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

Thanks Alex Shyba for your help with this one.

Exit mobile version