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.
- Bucket Rules
- Inheriting from IDynamicBucketFolderPath
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).
Comments
Post a Comment