It doesn't happen often, but every now and then it's useful to know precisely what is being published. Perhaps, from an auditing perspective, you want to have an auditable log of that information.
Ever wonder what gets published when you Publish subitems
or Publish related items
? Now you can find out. So let's do it.
Publishing Event
There are undoubtedly many ways to go about capturing this information. What I want to show you is but one way. In this case, when we think about the publishing process, we know that there are several events related to publishing. So naturally, this way is about attaching a processor to that cycle. We can see in the showconfig.aspx
the one we want is called publish:itemProcessed
. If we waited until publish:end
we would not be able to get the individual item information we want.
I've broken down the code below so you understand what's going on. You need to be aware though, this can be an expensive process to run. Especially if you know you're always publishing huge volumes of information. So with that in mind, you may care to disable the configuration for it until you need it.
using System;
using log4net;
using Sitecore.Publishing.Pipelines.PublishItem;
namespace App.Foundation.Publishing.Processors
{
class ItemProcessedProcessor
{
public void ItemProcessed(object sender, EventArgs args)
{
// 1. Convert the arguments into appropriate ItemProcessedEventArgs
ItemProcessedEventArgs itemProcessedEventArgs = args as ItemProcessedEventArgs;
// 2. Connect to the publishing context
PublishItemContext context = itemProcessedEventArgs != null ? itemProcessedEventArgs.Context : null;
if (context == null) return;
// 3. Set up custom logging or log to the existing Publishing log file
ILog log = Sitecore.Diagnostics.LoggerFactory.GetLogger("Sitecore.Diagnostics.Publishing");
// 4. Setup the database
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
// 5. Identify the item being processed
Sitecore.Data.Items.Item item = master.GetItem(context.ItemId);
// 6. Log any relevent information
if (item != null)
{
log.Info("**** App - Publishing Item: " + item.ID.ToString() + " " + context.Result.Operation.ToString() + " " + item.Paths.Path.ToString());
}
}
}
}
Configuration
Having the processor setup is but half the solution. We need to add it to the overall Sitecore configuration. So let's do that now.
Here we've created a patch file to add an event handler within the itemProcessed
event logic.
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv">
<sitecore role:require="Standalone or ContentDelivery or ContentManagement">
<events>
<event name="publish:itemProcessed">
<handler type="App.Foundation.Publishing.Processors.ItemProcessedProcessor, App.Foundation.Publishing" method="ItemProcessed">
</handler>
</event>
</events>
</sitecore>
</configuration>
And that's it. Super easy. You can now see what gets published when you publish sub items or related items.