In my 2011 Event System, I'm subscribing to two events:
EventSystem.Subscribe<Component, CheckInEventArgs>(OnComponentCheckedInPost, EventPhases.TransactionCommitted);
EventSystem.SubscribeAsync<Page, PublishEventArgs>(OnPagePublishPre, EventPhases.Initiated);
Inside OnComponentCheckedInPost
method, I have code that creates a page, attaches the component presentation to it and then finally, calls the following method that publishes the page:
public static void PublishPage(IdentifiableObject item, PublicationTarget publicationTarget, bool rollBackOnFailure, bool includeComponentLinks, PublishPriority publishPriority)
{
IEnumerable<IdentifiableObject> items = new List<IdentifiableObject> { item };
IEnumerable<PublicationTarget> targets = new List<PublicationTarget> { publicationTarget };
PublishInstruction instruction = new PublishInstruction(item.Session)
{
DeployAt = DateTime.Now,
MaximumNumberOfRenderFailures = 5,
RenderInstruction = new RenderInstruction(item.Session),
RenderInstruction.RenderMode = RenderMode.Publish,
ResolveInstruction = new ResolveInstruction(item.Session),
ResolveInstruction.IncludeComponentLinks = includeComponentLinks,
RollbackOnFailure = rollBackOnFailure,
StartAt = DateTime.MinValue
};
PublishEngine.Publish(items, instruction, targets, publishPriority);
}
Inside OnPagePublishPre
, I have code that checks the publication title and and then executes some additional logic:
private void OnPagePublishPre(Page page, PublishEventArgs args, EventPhases phase)
{
Publication publication = page.ContextRepository as Publication;
if (publication != null) {
if (publication.Title.ToLower == "my publication") {
//Do something
}
}
}
Both events trigger fine on their own. If I create/save a component NOT inside "my publication", it creates the corresponding page and publishes it successfully. Similarly, if I publish an existing page INSIDE "my publication", it successfully fires the OnPagePublishPre
method and executes the code.
The problems occurs when I create/save a component INSIDE "my publication" which creates and publishes the page, but DOES NOT trigger the OnPagePublishPre
method. I have verified this by attaching to the TcmPublisher.exe and TcmServiceHost.exe processes and stepping through the code. I have also tried different options for the second event: Subscribe instead of SubscribeAsync, TransactionCommitted instead of Initiated and all possible combinations as well as the EventSubscriptionOrder.
After further research, I was able to find a solution by instead subscribing to EventSystem.SubscribeAsync<Page, SetPublishStateEventArgs>(OnPagePublishPre, EventPhases.Initiated)
and then checking args.IsPublished
to see if the page was published or unpublished.
However, I would still like to know how to trigger the original subscription (if possible) - for my use case, I don't care if the page is actually successfully published or not. All I need to know is that it was sent to publish in order to execute the business logic.
- Why is the PublishPage code not firing
EventSystem.SubscribeAsync<Page, PublishEventArgs...;
? Is there anything missing with this code? - How is the 2011 event system working in this case? Does it have to do with the EventSubscription order or is there yet another subscription that I can use?
Even though I already have a solution, any insights are greatly appreciated.
OnPagePublishPre
is triggered, but it is not triggered by thePublishEngine.Publish(...)
call in yourPublishPage
method?SetPublishStateEventArgs
will not work always. This event is triggered only when the publishing is successful. When you mention that creating/saving a component not inside "my publication" creates and page and publishes it, are you sure that the events are also triggered? You mention in the second case that is the publish an existing page inside "my publication " it triggersOnPagePublishPre
not the earlier case.