2

How do I programmatically get an item on which I am performing publishing, irrespective of checkbox being checked (smart publish/ republish or publish related items or publish subitems)?

I don't want the whole publishing list just that item (the main item on which I clicked Publish->publish item). I have to make some code changes by modifying the publishing pipeline and not by event handler.

2
  • You find it.... in the publishing pipeline
    – Mark Cassidy
    Commented Nov 11, 2019 at 10:19
  • You can easily find this in Publishing Handler ...var rootItem = publisher.Options.RootItem; Commented Nov 11, 2019 at 11:04

1 Answer 1

5

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.

2
  • It worked . Thanks Sergey :) Commented Nov 12, 2019 at 11:48
  • I'm glad it worked, please click accept, if the answer was helpful.Thanks Commented Nov 12, 2019 at 12:37

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.