5
[TcmExtension("PageEventHandlerExtension")]
public class PageEventHandler : TcmExtension
{
    public PageEventHandler()
    {
        Subscribe();
    }

    public void Subscribe()
    {
        EventSystem.Subscribe<Page, PublishOrUnPublishEventArgs>(PagePublishOrUnpublishAction, EventPhases.Initiated);
    }

    private static void PagePublishOrUnpublishAction(Page page, PublishOrUnPublishEventArgs args, EventPhases phase)
    {
        if (page.Metadata==null)
        {
            throw new PublisherException(new LocalizableMessage(Resources.Properties.DoNotPublish));
        }
    }
}

In above method in Tridion Event System I am trying to forcefully terminate the publishing of a page.

While debugging I was able to hit the statement which is throwing the exception, but even then the item gets published successfully. Am I doing it the wrong way, if yes how can I fail the publishing of an item?

3
  • Not sure, that it will help, but you can try to clear in PublishOrUnPublishEventArgs Targets object (it has setter). It is taken to account during publishing. I think in this case even publishTransaction will not be created.
    – Syav4eg
    Jul 22, 2013 at 10:44
  • What do you see in the message center in the CME and what do the logs show, I.e. do you see you exception message show up anywhere? Jul 22, 2013 at 11:52
  • @NickoliRoussakov No I don't see exception thrown by me
    – AlokBhatt
    Jul 23, 2013 at 4:09

2 Answers 2

3

When using the following example, I'm perfectly able to stop publishing:

using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Publishing;
using Tridion.Localization;

namespace Example
{
    [TcmExtension("ExampleEventHandlerExtension")]
    public class EventHandler : TcmExtension
    {
        public EventHandler()
        {
            EventSystem.Subscribe<Page, PublishOrUnPublishEventArgs>(PagePublishOrUnpublishAction, EventPhases.Initiated);
        }

        private static void PagePublishOrUnpublishAction(Page page, PublishOrUnPublishEventArgs args, EventPhases phase)
        {
            if (page.Title.ToLower().Contains("do not publish"))
            {
                throw new PublisherException(new LocalizableMessage(Properties.Resources.ResourceManager, "DoNotPublishError", new object[] {page.Id}));
            }
        }
    }
}

In the details of the error it is not showing the ID of the Page, so not quite sure if I set the object[] correctly in the LocalizableMessage (you might have to play a bit with it, maybe it needs more objects, or the entire Page?).

By using Properties.Resources.ResourceManager I can just use the resources I have compiled in my DLL, which would allow for localizing the error message also.

Furthermore, be sure to register the DLL correctly in the Tridion.ContentManager.config and restart the SDL Tridion Content Manager COM+ application and Publisher and Service Host services (after deploying the DLL to the right location and updating the config, or after every rebuild of it).

8
  • When I debug this code it hits the break point but even then publishing is getting successful
    – AlokBhatt
    Jul 23, 2013 at 9:13
  • 2
    Do you get the error message shown in the UI at all? and what environment are you developing on, is it a single environment or an outscaled one? You might try a reboot just to make sure all references are updated. Jul 23, 2013 at 9:22
  • No I am not getting any error message on UI. My machine is 64 bit windows 7. CMS is SDL Tridion 2011 SP1. We have one CMS box and one CDS box. I have restarted COM+, Tridion Services. Even Killed eventing process in processes.
    – AlokBhatt
    Jul 23, 2013 at 12:04
  • sounds like something wierd is going on, with this code after registering it in Tridion.ContentManager.config and a restart of the COM= and services I directly get a message and the item isn't accepted in the publishing queue, so if you don't get that result, my guess is that you missed out on one of the steps or have a broken system Jul 23, 2013 at 12:07
  • Hey I want to confirm one thing. I have added my own resource file in solution and while creating Localizable message object I am passing resource name which I have created, Is this going correct?
    – AlokBhatt
    Jul 23, 2013 at 12:10
0

I am not exactly sure about the purpose for you want to fail the page publishing using event system. You could just create a simple TBB which will do your job, Instead for writing event handler,

Create a TBB something like Fail Publishing and then attach it to the page template.

[TcmTemplateTitle("Fail Publishing")]
 public class FailPublish : TemplateBase
    {
        public override void Transform(Engine engine, Package package)
        {
            base.Transform(engine, package);
            throw new Exception("REASON FOR MAKING THE PUBLISH FAIL")); 
        }
    }

you can check for some metadata field before throwing the exception.

2
  • If I have to check page metadata will this TBB attached to a PT will be able to handle?
    – AlokBhatt
    Jul 24, 2013 at 17:18
  • yes this can handle, you will have to read the metadata field before throwing the exception or you can also create a CT with this TBB, then create a dummy component and attach it to the page, this way you will have more control. You can do this in various ways, but it all depends on what you are trying to achieve.
    – Mohan
    Jul 24, 2013 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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