Event System handler code:
[TcmExtension("My Handler")]
public sealed class EventSystem : TcmExtension
{
public EventSystem()
{
EventSystem.Subscribe<Page, PublishEventArgs>((page, e, phases) => {
if (shouldTerminatePublishing(page))
{
throw new Exception(ex, page);
}
}, EventPhases.Initiated, EventSubscriptionOrder.Normal);
}
}
With the code above when multiple pages are being published and Event System is only about to block one of them (by throwing an exception), then all pages are effectively prevented from being published too. "Ignore Failures While Generating Publishable Content" check box does not affect this behavior.
How to prevent any given page from publishing but still allow all the rest to be published?
EDIT
Updated code as per Quirijn's suggestion:
public class MyResolver: IResolver
{
public void Resolve(
IdentifiableObject item,
ResolveInstruction instruction,
PublishContext context,
ISet<ResolvedItem> resolvedItems)
{
var page = item as Page;
if (null != page && instruction.Purpose == ResolvePurpose.Publish)
{
try
{
// Evaluate whether publishing is allowed
}
catch (Exception ex)
{
resolvedItems.Clear();
}
}
}
}
Some objections (or rather follow-up questions) so far:
EDIT 2
After looking into standard resolvers' implementation:
You shouldn't do this in the event system but in a custom resolver. This is a piece of code which gets executed to determine which pages / components should be effectively published when an item is put in the publish queue. Here you can filter out any page or component which you do not want to be published.
See How to remove items from publishing using a Tridion Resolver?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With