Extending Sitecore's PublishItemProcessor for Publish Datetime Retrieval

Learn how to tweak the publish pipeline to fetch the publish datetime in Sitecore XM Cloud

May 8, 2024

By Anju Thomas

Ever wondered when exactly an item was published in Sitecore? While the PublishItemProcessor handles publishing, it doesn't inherently capture the publish date and time. This blog post dives into how you can tweak this process to fetch and store that valuable information.

Here is the step-by-step process to achieve this:

How to Capture the Publish Date and Time

Create a "Published At" field

This is your first step. You'll need to create a new field named 'Published At' with the field type Datetime. It simply allows you to track when each page is published. The beauty of it is that this field is inherited in all Page Templates, making it a universal tracker across your site.

 Interface showing detailed options for scheduling content publication.

Create a Custom Class and Override the Process Method

Now, this is where things get a bit more technical. You're going to create a custom class that inherits from the Sitecore.Publishing.Pipelines.PublishItem.PublishItemProcessor class. This is just a class within Sitecore's publishing pipelines that we're going to leverage for our purposes.

Overriding the Process method is the crucial step. Here, you're going to override the Process method of the base class. Think of this as your playground, where you'll inject your custom logic. This is exactly where you're going to fetch the datetime when the publish pipeline process runs and write it to the Published At field.

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Publishing.Pipelines.PublishItem;
using Sitecore.SecurityModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace YourNamespace.Foundation.Workflows
{
    public class UpdatePublishedAtField : PublishItemProcessor
    {
        public override void Process(PublishItemContext context)
        {            
            var itemId = context.ItemId;            
            Database database = Sitecore.Configuration.Factory.GetDatabase("master");

            if (database != null)
            {
                Item item = database.GetItem(itemId);
                Sitecore.Diagnostics.Log.Info("Fetched Item " + item.Name, this);
                //Need to add the basetemplate id in the config
                var baseTemplateId = database.GetTemplate("{60FBBA72-9DE6-4838-A83F-3D33841CBF1B}");
                var itemTemplate = database.GetTemplate(item.TemplateID);
                if (itemTemplate != null && itemTemplate.BaseTemplates.Any(baseTemplate => baseTemplate.ID == baseTemplateId.ID))
                {
                    // Set the published at field value to the current date and time
                    using (new SecurityDisabler())
                        {
                            item.Editing.BeginEdit();
                            item["__Published At"] = DateTime.Now.ToString("yyyyMMddTHHmmssZ");
                            item.Editing.EndEdit();
                        }

                    Sitecore.Diagnostics.Log.Info("Updated Published At date of item " + item.Name, this);                    
                }
            }

        }
    }
}

This custom processor intercepts the publishing pipeline and checks if the item being published inherits from the specified base template. If it does, the processor captures the current date and time and updates the "__Published At" field on the item. This allows you to track the exact publish datetime for relevant items within your Sitecore environment.

Patching the Config

Register your custom processor in your Sitecore configuration to activate it within the publishing pipeline as provided below.

<sitecore>
        <pipelines>
            <edgePublishItem>
                <processor type="YourNamespace.Foundation.Workflows.UpdatePublishedAtField, YourNamespace.Foundation.Workflows" patch:after="processor[@type='Sitecore.ExperienceEdge.Connector.Client.Pipelines.EdgePublishItem.DetermineAction, Sitecore.ExperienceEdge.Connector.Client']" method="Process" resolve="true"/>
            </edgePublishItem>
        </pipelines>
    </sitecore>

Empowering Your Sitecore Workflow with Time-Stamped Publishing Insights

Follow these steps and you'll be able to keep an eye on when stuff gets published and learn a bunch about how your content's doing. It's not just about knowing when stuff went live, it can also help you automate things and make decisions based on cold hard facts.

Moreover we've also been using this 'Published At' field in workflows and even in custom content editor warnings - think of it as a publishing info for pages. You can read about it in this blog.

Thanks for sticking around till the end!



Anju Headshot

Anju Thomas

Sitecore Web Developer

Anju is a Sitecore Developer with a Bachelor's Degree in Computer Science and over 4 years of experience in Sitecore development. She loves solving Sudoku puzzles, watching movies, eating delicious food and exploring the world.