1

I have below situation where, I have to prevent OnItemProcessed event from occuring more than once.

namespace Website
{
    public class ItemEventHandler
    {
        public void OnItemProcessed(object sender, EventArgs args)
        {
            // Code here will be processed once for every item in that publish event.



            if(condition){
            Queue.WriteMessage();
            }

            // once message is written to Queue based on condition, this event handler should not be run for subsequent items in that publish event
        }
    }
}

events config patch

<sitecore>
    <events>
      <event name="publish:itemProcessed">
        <handler type="Website.ItemEventHandler, Website" method="OnItemProcessed">
        </handler>
      </event>
    </events>
  </sitecore>
  • Is your event handler configured to run on item:published event? So you want to write to Queue only for the first item published? E.g. you publish item with 2 children, and you want to write to Queue only for the parent item, not for the children, correct? – Marek Musielak May 23 '18 at 11:56
  • @MarekMusielak.. the event handler is configured on "publish:itemProcessed". For ex I have 10 items in particular publish event, and on 5th item, condition is satisfied and message is written to queue, it should not process the event handler for subsequent items(i.e 6-10) – nari447 May 23 '18 at 12:13
  • 1
    Try to add a public static bool variable in the ItemEventHandler class, and set it to true when the condition is satisfied. Then add another event handler on publish:end which will reset the variable back to false. It should do the trick assuming that you have only 1 Sitecore instance publishing items at the same time and you're not using publishing service. – Marek Musielak May 23 '18 at 12:32
  • don't forget to let us know if this solution worked for you – Marek Musielak May 23 '18 at 13:33
  • sure @MarekMusielak – nari447 May 24 '18 at 5:25
1

Add a public static bool variable in the ItemEventHandler class and set it totrue when the condition is satisfied.

Then add another event handler on publish:end which will reset the variable back to false.

It will do the trick assuming that you have only 1 Sitecore instance publishing items at the same time and you're not using publishing service.

| improve this answer | |

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.