Buckets and Extending Bucketing Behavior


The Problem

In a recent project, we needed to specify how a bucketable item is to be bucketed. Luckily Sitecore provides a rule-based mechanism to configure bucketable item paths. Unfortunately, the requirements would not allow us to use the action provided from Sitecore out of the box. However, since Sitecore is so extensible, we can implement a custom bucket rule action with relatively little effort.

The Solution

Sitecore provides a rule-based mechanism to configure how it builds bucketable item paths. The field used to define rule(s) is “Rules for Resolving the Bucket Folder Path:” It can be found on the “Item Buckets Settings” item under the following path in Sitecore 8.2, /sitecore/system/Settings/Buckets

Figure 1

The requirements for this feature calls for this bucketable item to be bucketed on a date field defined on the item template. However, the actions available are:

  • Based on Item Creation Date
  • Based on Item ID
  • Based on Item Name

Figure 2

No worries, since Sitecore is so extensible, we can create our own bucket rule action with very little effort. First, we need to write the class that will execute as the action.

public class DateFieldBasedPath : RuleAction where T: BucketingRuleContext
{
  public ID FieldId { get; set; }
  public string Format { get; set; }
  public override void Apply(T ruleContext)
  {
    Assert.ArgumentNotNull(ruleContext, nameof(ruleContext));
    var format = Format;
    if (string.IsNullOrEmpty(format))
    {
      format = BucketConfigurationSettings.BucketFolderPath;
    }
   
   var date = ruleContext.CreationDate;
   var item = ruleContext.BucketItem.Database.GetItem(ruleContext.NewItemId);
   if (item == null)
   {
     return;
   }
   
   DateField dateField = item.Fields[FieldId];
   if (dateField != null)
   {
     if (dateField.DateTime != DateTime.MinValue)
     {
       date = dateField.DateTime;
     }
   }
   ruleContext.ResolvedPath = date.ToString(format, Context.Culture);
   }
}

Next, we’ll need to define a new action in Sitecore. Create a new Action, Resolve Date Field Based Path, under /sitecore/system/Settings/Rules/Definitions/Elements/Bucketing.

Figure 3

Once completed, you’ll have access to it when creating a Rule for Resolving the Bucket Folder Path.

Figure 4

Now when the News Landing bucket is sync’ed, all the bucketable items under it will be organized based on the date in the Display Date field.

Categories: Sitecore
Tags: Sitecore, Net;

SEARCH ARTICLES

CATEGORIES

Sitecore 144
Commerce 100
Web Development 100
Sitecore Commerce 83
Sitecore Experience Commerce 9 64
Sitecore Experience Commerce 59
Content Management 55
eCommerce 48
B2B eCommerce 42
Sitecore Experience Platform 39
Sitecore Platinum Partner 39
Architecture 34
Insite 29
User Experience 26
Strategy 22
B2C eCommerce 21
B2B Commerce Blogs 21
commerceconnect 21
CloudCraze 20
SaaS (Software as a Service) 20
Cloud 17
Commerce Server 17
Salesforce B2B Commerce Cloud 16
Mobile 13
Search 13
Plugins 12
Analytics 12
Application Development 12
Digital Transformation 11
Sitecore Symposium 11
SPEAK 10
Helix 10
DMS 8
Social 8
Business Process 7
Authentication 7
5 Reasons 7
BI and Big Data 6
Data Visualization 6
Sitecore Presentation 6
Coveo 6
NET Development 5
Microsoft Azure 5
Automation 5
Front-End Development 4
SaaS (Software as a Service) 4
Digital Strategy 4
Brightcove 4
Press Release 4
Avalara Tax 3
Sitecore Experience Accelerator (SXA) 3
Sitecore Layouts 3
Video 3
SPE 3
Multi-site 3
Multi-lingual 3
Accessibility 3
Habitat 3
Vault 3
Identity 2
Managed Services 2
CDN 2
SMB 2
Cryptocurrency 2
Sitecore Forms 2
Sitecore Experience Commerce 9 Promotions 2
Uncategorized 2
EXM 2
Conversational Commerce 2
Sitecore SaaS 2
Security 2
Unit Testing 2
Headless Architecture 2
Sitecore Experience Awards 2
Google 1
Content Delivery Network 1
Configure Price Quote 1
CPQ 1
Blockchain 1
Coupons 1
Sitecore Rss 1
Artificial Intelligence 1
Machine Learning 1
Okta 1
RFP Process 1
NoSQL 1
Flex Accelerator for Sitecore 1
Reviews 1
SEO 1
Page Labels 1