1

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 gets executed every time but it doesn't update the field every time. Only after the apppool 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="RACQ.Feature.CarAdvisory.Pipelines.CustomCacheUpdatePublishPipeline, RACQ.Feature.CarAdvisory" method="UpdatePublishingDate" />
            </event>
        </events>
    </sitecore>
</configuration>
1
  • 1
    I just want to say that it never ends well when you try to edit a value in a field during publishing. You are better off creating a proper process to indicate the desired DateTime of that Sitecore item, either at the item creation / update, or at the time of approval in workflow. May 4, 2023 at 0:29

1 Answer 1

1

The problem is with this line

using (new EditContext(configurationItem, false /*updateStatistics*/, true /*silent*/))

You use true for silent which means that no events should be triggered. That also means that Sitecore will not clear its caches.

And btw, EditContext is obsolete. Just use

item.Editing.BeginEdit()
...
item.Editing.EndEdit()
1
  • setting silent to false and update statistics to true does the trick.
    – Arun Kumar
    May 17, 2023 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.