Saturday, 19 July 2014

Configuring Sitecore Item Buckets with different bucket folder path : Part 2

Leave a Comment
In my previous post, I’ve explained about changing the bucketing strategy by using predefined bucketing rules. In this post I am going to explain how to implement bucketing strategy by writing custom code.

Bucketing Strategy: Using Custom Code

Now I demonstrate how newly created bucketable items will be auto organized into content tree based on item name with dictionary index hierarchy with up to two levels. I’ll write custom code to achieve this behavior.
In Sitecore, create a template field titled Enable Custom Dynamic Bucket Folder Path as a checkbox field to /sitecore/templates/System/Templates/Sections/Item Buckets.
I’ve created a bucket folder named Article and enable checkbox for Enable Custom Dynamic Bucket Folder Path field in Item Buckets section.
I’ve written a CustomBucketFolderPathResolver class which implements IDynamicBucketFolderPath interface. In the function GetFolderPath, I am checking value of Enable Custom Dynamic Bucket Folder Path checkbox. If it is true then I am creating custom dynamic bucket folder path else path will be auto generated based on Bucketing Rule Context or by the creation date of the item.
Below is the code of CustomBucketFolderPathResolver class:
using Sitecore;
using Sitecore.Buckets.Rules.Bucketing;
using Sitecore.Buckets.Util;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.StringExtensions;
using System;

namespace Sitecore.Ramblings
{
    public class CustomBucketFolderPathResolver : IDynamicBucketFolderPath
    {
        public CustomBucketFolderPathResolver()
        {
        }

        public string GetFolderPath(Database database, string itemName, ID templateId, ID itemId, ID parentItemId, DateTime creationDateOfNewItem)
        {
            Assert.ArgumentNotNull(database, "database");
            Assert.ArgumentNotNull(itemId, "itemId");
            Assert.ArgumentNotNull(parentItemId, "parentItemId");

            string resolvedPath = null;

            Item parentItem = database.GetItem(parentItemId);

            if (parentItem != null)
            {
                if (parentItem.Fields["Enable Custom Dynamic Bucket Folder Path"].Value == "1")
                {
                    if (itemName.Length == 1)
                    {
                        resolvedPath = itemName;
                    }
                    else
                    {
                        resolvedPath = itemName.Substring(0, 1) + "/" + itemName.Substring(0, 2);
                    }
                    return resolvedPath;
                }
            }

            BucketingRuleContext bucketingRuleContext = new BucketingRuleContext(database, parentItemId, itemId, itemName, templateId, creationDateOfNewItem)
            {
                NewItemId = itemId,
                CreationDate = creationDateOfNewItem
            };
            BucketingRuleContext bucketingRuleContext1 = bucketingRuleContext;
            Item item = database.GetItem(Sitecore.Buckets.Util.Constants.SettingsItemId);
            Assert.IsNotNull(item, "Setting Item");
            Item[] itemArray = new Item[] { item };
            RuleList<BucketingRuleContext> rules = RuleFactory.GetRules<BucketingRuleContext>(itemArray, Sitecore.Buckets.Util.Constants.BucketRulesFieldId);
            try
            {
                if (rules != null)
                {
                    rules.Run(bucketingRuleContext1);
                }
            }
            catch (Exception exception)
            {
                Log.Error(string.Format("BucketFolderPathResolver: Cannot resolve bucket path for item {0}. Parent = {1}", itemId, parentItemId), exception);
            }

            resolvedPath = bucketingRuleContext1.ResolvedPath;
            if (resolvedPath.IsNullOrEmpty())
            {
                resolvedPath = creationDateOfNewItem.ToString(BucketConfigurationSettings.BucketFolderPath, Context.Culture);
            }
            return resolvedPath;
        }
    }
}
Go to App_Config\Include\Sitecore.Buckets.config file and set value of BucketConfiguration.DynamicBucketFolderPath setting as below:
<!--<setting name="BucketConfiguration.DynamicBucketFolderPath" value="Sitecore.Buckets.Util.BucketFolderPathResolver, Sitecore.Buckets"/>-->
<setting name="BucketConfiguration.DynamicBucketFolderPath" value="Sitecore.Ramblings.CustomBucketFolderPathResolver, SitecoreRamblings "/>
The value format is “<namespace>.<class name>, <assembly name>”. For example:Sitecore.Ramblings is namespace, CustomBucketFolderPathResolver is class name and SitecoreRamblings is assembly name.

Create a new item under Article bucket folder and it will auto organized into content tree based on item name with dictionary index hierarchy with up to two levels. Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a Comment