6

I am performing some logic after successful publishing of each page on Live target and hence I have subscribed to the following event:

EventSystem.SubscribeAsync<PublishTransaction, SetPublishStateEventArgs>(
                                                    CustomLogic, 
                                                    EventPhases.TransactionCommitted);

My custom logic works well but now I want to change the logic based on the Published/Republished state of the page.

I tried getting the ResolvePurpose using following:

PublishTransaction.Instruction.ResolveInstruction.Purpose

But it’s only giving the Publish or Unpublish state but not Republish.

The other option is to change the event Phase to EventPhases.Initiated, and I make a check if page.IsPublishedInContext but that too will always return true even if the page is Published to Staging and also my custom logic I believe will always gets invoked even if the Publishing fails which I don’t want.

Any help would be appreciated.

Thanks in advance.

  • Can someone provide any pointer, I believe this should be a very common use case and many of you would have already faced this situation. Thanks – Yash Oct 25 '16 at 9:17
  • Did you tried to subcribe to the following event: EventSystem.SubscribeAsync<PublishTransaction, PublishEventArgs>(CustomLogic, EventPhases.TransactionCommitted); And then try to check PublishEventArgs.PublishInstruction.ResolveInstruction.Purpose – Stefan Klasnic Oct 25 '16 at 12:03
  • Hi @StefanKlasnic, seems the PublishEventArgs doesn't go with PublishTransaction, I tried but my custom code itself didn't execute. – Yash Oct 25 '16 at 17:19
1

Given below is the solution for the problem:

Subscribe to the event

EventSystem.Subscribe<PublishTransaction, SaveEventArgs>(
                                                  OnPublishStatus_Initiated, 
                                                  EventPhases.Initiated);

Check for transaction.State is success

transaction.State == PublishTransactionState.Success

Inside the success check condition, use following condition to handle logic for different publish action

transaction.Instruction.ResolveInstruction.Purpose == ResolvePurpose.Publish
transaction.Instruction.ResolveInstruction.Purpose == ResolvePurpose.RePublish

Please note, if you have scaled out CM and publisher (i.e. on separate servers) then this event system should be deployed on publisher.

We are using this code to verify the Publish and Unpublish and it is working fine.

Other option / work around: Since you have mentioned that you are getting RePublished state in Initiated state, there is one thing that you can do.

Register two event handlers

  1. Initiated phase: which will log the information of transaction id and state Publish / RePublish at the centralized location.
  2. TransactionCommitted: This will check that transaction is successful and if it is successful then it will get the information from the centralized location whether it is publish or republish

Centralized location can be database, context variables etc. Please note, for context variables to store data your event handlers should be synchronous.

  • Thanks @Hiren, I am also getting the Publish & Unpublish status without any issue but for Republishing of page, it always gives the status as "Publish". – Yash Oct 26 '16 at 17:26
  • 1
    There are two different events, what you have is SetPublishStateEventArgs and what I have mentioned is SaveEventArgs. Hope you have tried that. – Hiren Kaku Oct 26 '16 at 17:30
  • Hi @Hiren, I have just tried the approach you suggested but no luck there too I am getting "Publish" instead of "RePublish". Thanks. – Yash Oct 26 '16 at 21:18
  • @Yash - I think there is workaround based on your question. If that is not working or if you need some other solution then the last option is to create the support ticket. – Hiren Kaku Oct 26 '16 at 21:42
  • 1
    Hi @Hiren, the workaround also doesn't give the status as RePublish, what I meant in an alternate approach was that I can check if page.IsPublishedInContext but that too won't work in my case as it always return true even if the page is Published to Staging. – Yash Oct 27 '16 at 7:40

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.