This article describes how to display the list of published items right after the publishing has finished. There are two ways to do this:
- Use PublishLog
- Derive a new class from PublishLog class and override necessary methods
- Add custom event handler to “publish:begin” event and hook custom PublishLog to the publisher instead of native PublishLog.
- Use PublishEngine
- Create a class with appropriate event handlers
- Create a hook where you can assign an event handler to corresponding PublishEngine.
The present article describes the latter approach. The former approach is outlined here:
http://sdn.sitecore.net/Scrapbook/Change%20items%20during%20the%20publishing%20process.html
1. Creating a class with event handlers
The following events can be used to track the changes made by the publishing process.
-
void ItemAdded(object sender, Sitecore.Data.Events.ItemAddedEventArgs args)
Catches an added item (it will be fired only for a newly added item and only if at least one version of the item is publishable)
-
void ItemProcessed(object sender, Sitecore.Data.Events.ItemProcessedEventArgs args)
Catches a processed item
-
void ItemProcessing(object sender, Sitecore.Data.Events.ItemProcessingEventArgs args)
Catches an item which is being processed.
-
void ItemRemoved(object sender, Sitecore.Data.Events.ItemRemovedEventArgs args)
Catches a deleted item
-
void VersionAdded(object sender, Sitecore.Data.Events.VersionAddedEventArgs args)
Catches an added version
-
void VersionsRemoved(object sender, Sitecore.Data.Events.VersionsRemovedEventArgs args)
Catches a removed version
You can use one of the methods listed above or their combination to achieve the desired functionality.
The example below shows how to catch all items which have been added to or changed in the web database. Algorithm notes: the code gets the items which have been added to or changed in web database by catching the VersionAdded event. In this way the code catches all changes made to the last accessible version of an item. For instance, the ItemAdded event of PublishEngine class is fired only when a newly created item is published to web database. This method will not be fired if a new version of an item is created and published.
So, we will use the VersionAdded event.
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
namespace Statistics.Publish
{
public class GetPublishedItems
{
internal static void PublishEngine_VersionAdded(object sender, Sitecore.Data.Events.VersionAddedEventArgs args)
{
Item version = args.Item;
Log.Info(string.Format("Item: {0}, Version: {1}, Language: {2}", version.Paths.Path, version.Version.Number, version.Language.Name), this);
}
}
}
Create a hook:
namespace Statistics.Publish
{
class PublishEngineHook
{
public void Initialize()
{
Database database = Sitecore.Context.ContentDatabase;
if (database == null)
{
database = Sitecore.Context.Database;
}
if (database != null)
{
database.Engines.PublishEngine.VersionAdded += new EventHandler<Sitecore.Data.Events.VersionAddedEventArgs>(Statistics.Publish.GetPublishedItems.PublishEngine_VersionAdded);
}
}
}
}
Compile your solution and add hook to the web.config file under the <hooks> section as shown below:
.............
<hook type="Statistics.Publish.PublishEngineHook, GetPublishedItems" />
</hooks>
Restart the IIS site to make Sitecore initialize the hook.
Related reading: