In a publish pipeline processor, in order to get an item on which publishing is performed (the root item), you can use PublishContext
and it's PublishOptions.RootItem
property.
The class should be inherited from Sitecore.Publishing.Pipelines.Publish.PublishProcessor
.
Note: You can get RootItem
when "Publish Item"
operation is used, but the property will be null
when "Publish Site"
is triggered.
The following example shows how to get publishing RootItem
in a publishing pipeline processor:
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Publishing.Pipelines.Publish;
namespace CustomProcessors
{
public class YourPublishProcessor : PublishProcessor
{
public override void Process(PublishContext context)
{
Assert.ArgumentNotNull(context, "context");
//this is the item on which "Publish item" operation was triggered
Item rootItem = context.PublishOptions.RootItem;
}
}
}
You can also find a bit more information in this blog post.
The RootItem
will be available regardless of what publishing options are selected (smart publish/ republish or publish related items or publish subitems), but only available when "Publish Item"
is triggered.