Fixing Sitecore Buckets folder path - Items created after 12 AM server time zone

We are working on a 24/7 content publishing site, so it is important to have articles published on the correct date and time. 

We are using Sitecore buckets to manage article items.

In the Sitecore content tree, when the content author tries to look at the items created after 12 AM server time, the items are created under the previous day folder only,  but it is supposed to be created in the current day folder(ie. new bucket day folder to be created and the items to be added in this new day folder).

We have hosted the site in Azure VM in the UAE region.

UAE is 4 hours ahead of GMT/UTC (UTC+04:00) so items created between 12 AM to 4 AM are always created under the previous day's folder, but when the item created after 4 AM is properly created under the current day folder in the content tree.

Example:

If the item was created on 2024 à09 à24 between 12 AM to 4 AM then still the item is made under the bucket folder of 2024 à 09à23

The created date and the updated date of the Sitecore item are also pointed to the previous date folder.

The screenshot shows the issue.

I have tried to update the following setting to make the Sitecore follow the ServerTimeZone instead of the default UTC timezone, but still I see the items created under the previous folder.

<setting name="ServerTimeZone" value="Arabian Standard Time" >

I tried different approaches to fix using the configuration way but had no luck, so I tried to customize the buckets folder structure.

By default, the folder structure of the buckets is defined in the Sitecore settings defined in Sitecore.Buckets.config.


So to customize the bucket folder structure to use ServerTimeZone instead of UTC, there are different ways to customize the Sitecore bucket structure using either one of the below approaches.
  • Bucket Rules
  • Inheriting from IDynamicBucketFolderPath
I have used the IDynamicBucketFolderPath interface to customize the bucket structure.     
In the below code, I have specifically targeted the template items that need to be created based on the Server Time zone. But for the other templates, the default UTC zone works.
 public class CustomBucketFolderStructure : IDynamicBucketFolderPath
 {
     public string GetFolderPath(Database database, string name, ID templateId, ID newItemId, ID parentItemId, DateTime creationDateOfNewItem)
     {
         string resolvedPath = null;
         if (templateId == ID.Parse(TemplateReferences.NewArticleTemplateID))
         {
             var itemCreatedDate = Sitecore.DateUtil.ToServerTime(creationDateOfNewItem);
             return resolvedPath = itemCreatedDate.ToString(BucketConfigurationSettings.BucketFolderPath, Context.Culture);
         }
         BucketingRuleContext bucketingRuleContext = new BucketingRuleContext(database, parentItemId, newItemId, name, templateId, creationDateOfNewItem)
         {
             NewItemId = newItemId,
             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}", newItemId, parentItemId), exception);
         }

         resolvedPath = bucketingRuleContext1.ResolvedPath;
         if (!String.IsNullOrEmpty(resolvedPath))
         {
             resolvedPath = creationDateOfNewItem.ToString(BucketConfigurationSettings.BucketFolderPath, Context.Culture);
         }
         return resolvedPath;
     }
 }  

       
 
Add the below setting to refer to the above-created code to create the bucket folder structure based on the server time zone.
<setting name="BucketConfiguration.DynamicBucketFolderPath" value="Namespace.Pipeline.CustomBucketFolderStructure, Foundation.Logic" />
After the settings, the bucket folder structure works perfectly by creating the items under the specific folder as per the below example the item was created on 03/11/2024 at 1 AM (I manually changed the server date to 1 AM to replicate this behavior temporarily).




Note:

But if looking for a more configurable way from Sitecore CMS, use bucket rules to update the bucket path of the settings  BucketConfiguration.DynamicBucketFolderPath.

Reference link to use the bucket rules:

Let's learn and grow together, happy programming 😊

Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview

Sitecore Custom Rule (Action and Condition)