I am trying to update an item inside custom publish pipeline in sitecore 10.2
. It does get updated once but if pipeline runs again the item is not updated.
The item update code get executed everytime but it doesn't update the field everytime. Only after app pool reset it does it once and nothing after that even if the code is executed.
Below is my pipeline code:
public class CustomCacheUpdatePublishPipeline
{
public void UpdatePublishingDate(object sender, EventArgs args)
{
var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;
if (sitecoreArgs == null)
return;
var publisher = sitecoreArgs.Parameters[0] as Publisher;
if (publisher == null)
return;
var db = Sitecore.Configuration.Factory.GetDatabase("master");
Item item = publisher.Options.RootItem;
Item configurationItem = db.GetItem(Templates.CarAdvisoryConfiguration.ItemId);
if (item != null && configurationItem != null)
{
ID[] templateIds = configurationItem[Templates.CarAdvisoryConfiguration.Fields.TemplateIds]?.Split('|')?.Select(x => new ID(x))?.ToArray();
if ((templateIds?.Any() ?? false) &&
templateIds.Contains(item.TemplateID) || (item.Axes?.GetAncestors()?.Where(x => templateIds.Contains(x.TemplateID))?.Any() ?? false))
{
using (new EditContext(configurationItem, false /*updateStatistics*/, true /*silent*/))
{
configurationItem.Fields[Templates.CarAdvisoryConfiguration.Fields.LastUpdated].Value = DateUtil.ToIsoDate(System.DateTime.Now); //this only get updated once and doesn't get updated after say 2 mins
}
Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Database web = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(master,
web,
Sitecore.Publishing.PublishMode.SingleItem,
configurationItem.Language,
System.DateTime.Now);
Sitecore.Publishing.Publisher publisher1 = new Sitecore.Publishing.Publisher(publishOptions);
publisher1.Options.RootItem = configurationItem;
publisher1.Options.Deep = true;
publisher1.Publish();
}
}
}
}
config:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="publish:end">
<handler type="Feature.CarAdvisory.Pipelines.CustomCacheUpdatePublishPipeline, Feature.CarAdvisory" method="UpdatePublishingDate" />
</event>
</events>
</sitecore>
</configuration>