0

I am trying to build a custom publishing pipeline in SC 9.3 where I need to do some custom operations on the items being currently published.

I created a custom pipeline and the code looks like below:

public class MyProcessor : PublishItemProcessor
{
    public override void Process(PublishItemContext context)
    {
            if (context == null || context.Aborted)
            {
                return;
            }
            var item = Sitecore.Context.Item;
            Item sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);
            Item targetItem = context.PublishHelper.GetTargetItem(context.ItemId);
            ...
    }
 }

Unfortunately, I ran out of ideas on how to get the currently published item ID inside this pipeline. In the code above the item is null, and the sourceItem and targetItem return the language of the content, not the actual item that I am publishing.

Could you please help and point me out to the best way how to check which item is being published?

2

You can achieve this by adding a handler to "publish:itemProcessed" event instead:

public void YourItemProcessed(object sender, EventArgs args)
{
    var itemProcessedEventArgs = args as ItemProcessedEventArgs;
    var context = itemProcessedEventArgs != null ? itemProcessedEventArgs.Context : null;

    if (context.Result.Operation.Equals(PublishOperation.None) ||
        context.Result.Operation.Equals(PublishOperation.Skipped))
    {
        return;
    }
    try
    {
        // Your logic here...
    }
    catch (Exception e)
    {
        Sitecore.Diagnostics.Log.Error($"Error at publish:itemProcessed. Item ID: {context.ItemId} Name: {context.ItemName} => {e.InnerException}", this);
    }
}

Then patch it:

<events>
    <event name="publish:itemProcessed">
      <handler type="Your.Project.YourItemProcrssed, Your.Project" method="YourItemProcessedLogic"/>
    </event>
</events>

I hope it helps you..

1

Actually context.PublishHelper.GetSourceItem and context.PublishHelper.GetTargetItem are the right methods to use.

What they do in the background, they take Options.Language and Options.SourceDatabase (or Options.TargetDatabase for target item) and call GetItem using those parameter together with item id.

When you select an item in Content Editor and choose some language version and click publish, you have an option to choose which languages will be published. And your processor should be executed for every language chosen in the options. It won't be executed on the version that you selected in Content Editor unless you choose that language in the options window.

2
  • I do not want to get the current language but the actual page item I am trying to publish and these methods do not seem to be working as I want them to. How can I do that? Or different pipeline should be used?
    – 9_9
    Oct 15 '21 at 11:43
  • What are you actually asking? if you have the Item instantiated in the language being processed - or any language at all - you have the item ID
    – Mark Cassidy
    Oct 15 '21 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.