4

I'm attempting to register a custom event handler to SDL Web 8.5.

[TcmExtension("ResponsiveTableEvents")]
public class ConditionalEvents : TcmExtension
{
    private const string SchemaRootElementName = "table";

    public ConditionalEvents()
    {
        Subscribe();
    }

    public void Subscribe()
    {
        EventSystem.Subscribe<IdentifiableObject, SaveEventArgs>(SaveItem, EventPhases.Initiated);
    }

    private void SaveItem(IdentifiableObject item, SaveEventArgs args, EventPhases phases)
    {
        var component = item as Component;
        if (component == null) return;
        if (component.Schema.RootElementName == SchemaRootElementName)
        {
            TableComponent.Process(component);
        }
    }
}

I followed the official documentation here. Once completed, I stopped the IIS and SDL Web Content Manager Service Host services. I dropped the compiled DLL in %TRIDION_HOME%/bin/. I updated the Tridion.ContentManager.config to include:

<extensions>
    <add assemblyFileName="C:\Program Files (x86)\SDL Web\bin\My.Custom.EventHandlers.dll" />
</extensions>

I restarted the services. I verified that the event handler has been loaded by checking Event Viewer/Applications and Services Logs/Tridion.

The customer event handler logic is to throw an exception if validation is not met - for example, depending on the type selected in one field, it requires a specific field to be mandatory.

I created a Table component and saved the changes knowing that the custom event handler should throw an error but it doesn't. The code to throw an error is:

throw new Exception("Test Error Message!");

What I've tried:

  • Restarted all SDL services
  • Restarted COM+ System Application service
  • Checked Event Viewer, no error
  • Check cd_core logs, no error
  • Commented out my logic, and simply throw an exception on the custom event handler method "SaveItem" and re-deployed but still nothing.

Are there anything else that I could to do to troubleshoot or have I misunderstood an instruction from the documentation?

6

From looking at the code there are a few remarks which I will make, but looking at your description I'm not directly seeing reasons why it shouldn't work. If you only have a single CM server (not an outscaled scenario) then updating the Tridion.ContentManager.config and restarting the SDL Web Content Manager Service Host service should do the trick.

The few issues I see with the code are the following;

  1. You subscribed to <IdentifiableObject, SaveEventArgs>(SaveItem, EventPhases.Initiated) which means this will trigger on save of any Identifiable Object, so not just Components. Hence the following line in your code will cause an issue var component = item as Component;, and it will probabaly trigger the silent return via if (component == null) return;.

    I'm always saying you should never use as Component, it's always better to cast (Component component = (Component)item; which would throw an Exception if the item is not a Component), but in your case it's better to simply narrow down the subscribe. I would suggest to change your code into:

    public void Subscribe()
    {
        EventSystem.Subscribe<Component, SaveEventArgs>(SaveComponent, EventPhases.Initiated);
    }
    
    private void SaveComponent(Component subject, SaveEventArgs args, EventPhases phases)
    {
        if (subject.Schema.RootElementName == SchemaRootElementName)
        {
            TableComponent.Process(subject);
        }
    }
    
  2. Then for the exception, since your are subscribed to the Initiated phase, you can actually raise a localizable Exception (Tridion.Localization.LocalizableMessage) and get a messige in the GUIs language (provided you have a resource bundle with translations of the message. But in general, simply throwing an Exception should do the trick of stopping the save event.

As to how to further trouble shoot, You can try to debug the event handler by attaching to the TcmServiceHost.exe and the w3wp.exe process (the latter is the one from the IIS site your CME is running under) from Visual Studio (see also http://docs.sdl.com/LiveContent/content/en-US/SDL%20Web-v5/GUID-CE0F74C6-A6F3-44EC-B929-CF81B8383D8E).

  • Thanks Bart! I changed the subject and used direct casting - one of my cast in the TableComponent.Process was returning null which gave the impression that it wasn't working. I've also started using InvalidActionException with localizable message. – Tim Alonso Jul 12 '17 at 14:03

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.