Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Christoffersen 64 posts 119 karma points
    Aug 14, 2009 @ 11:07
    Dan Christoffersen
    1

    How to use event handlers on custom document types?

    I must completly have missed something. It might be that I'm new to both Umbraco and .net, but I can't seem to figure out how use the event handlers on custom document types. Actually the problem is that I can't figure out how to implement and use the custom document types in Umbraco in the first place. Maybe I have got it all wrong?!

    Can anyone please point me in the right direction, or maybe just in the direction of where to find the documentation for it (if any)?

    Cheers,
    Dan

    Ps. : This is probably one of those post that has such a simple solution that I end up being embarred about ever posting it, so please be gentle ;-)

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 14, 2009 @ 11:21
    Thomas Höhler
    0

    First take a list on the available event handlers: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events

    If you want to react on the publishing of a content item from a special document type just implement the BeforePublish event handler and check for the document type alias of the document which will be saved.

    hth, Thomas

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 14, 2009 @ 11:21
  • Dan Christoffersen 64 posts 119 karma points
    Aug 14, 2009 @ 11:32
    Dan Christoffersen
    0

    I think you might have misunderstood my problem. I have read the examples but don't get how they will end up working in my umbraco application. My "issue" is probably a much more fundamental one.

    Making a class with the functions that handles the event - no problem. Compiling it to a dll - no problem. Uploading the dll to the Umbraco installation dir/bin - no problem. Where to go from there - big problem ;-)

    As mentioned before, I might have got the Idea of how it works all wrong.

    /Dan

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 14, 2009 @ 11:44
    Thomas Höhler
    100

    Oh, so I misunderstood you. So let me have another try:

    As I understand right you have implemented an event handler which is doing something on publishing a document?

    If so and you have uploaded the dll to the bin folder the umbraco installation will automatically reinitialize (it is a .Net feature that if you change something in the bin folder rthe app reinitializes). Umbraco then will go throught the included dlls via .NET reflection and will search for all event handlers and registers them. So if you have uploaded an event handlers dll it will work automatically.

    Could I help you with that or do you have another problem?

    Thomas

  • Dan Christoffersen 64 posts 119 karma points
    Aug 14, 2009 @ 12:07
    Dan Christoffersen
    0

    You nailed it Thomas.

    But won't the publish event be executed no matter what document type is published? If so, I guess the trick is to check for the document type?

    /Dan

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 14, 2009 @ 12:17
    Thomas Höhler
    0

    You are right, but you can check the document type alias of the document which you want to react to:

    void Document_BeforePublish(umbraco.cms.businesslogic.web.Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
    {
    if (sender.ContentType.Alias == "YOUR_DOC_TYPE_ALIAS")
    {
    // Do your tasks
    }
    }

    In the if statement you are doing the tasks you only wnat to do for this document type

    If you want to cancel the whole publishing process of this node you can add e.Cancel = true;

    hth, Thomas

  • Dan Christoffersen 64 posts 119 karma points
    Aug 14, 2009 @ 12:27
    Dan Christoffersen
    0

    Thank you for your help Thomas. I will try that.

    Cheers
    /Dan

  • Qube 74 posts 116 karma points
    Apr 23, 2010 @ 04:31
    Qube
    0

    I'm playing around with similar stuff, but instead of checking for a particular DocumentType, I need to know if my custom DataType is being used in the DocumentType of the current Document. If so, I need to write some XML to the file system based on the value of the respective Property.

    Any pointers on how I'd find only my custom DataType? Is there a more elegant way than iterating through like this?

    private void Document_AfterPublish(Document sender, EventArgs e)
    {
        foreach (Property DOCPROP in sender.getProperties)
        {
            if (DOCPROP.PropertyType.DataTypeDefinition.DataType is MY.DATA.TYPE)
            {
                // TODO: Write XML to file system
            }
        }
    }

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 23, 2010 @ 10:36
    Dirk De Grave
    1

    Hi,

    Me thinks it can be done using Linq

    var properties = sender.getProperties;
    var datatypes = from p in properties where p.PropertyType.DataTypeDefinition.DataType.Id.ToString() == myDataType.Id.ToString());
    if (datatypes.Length == 1) { // datatype found }
    

    Hope this helps.

    Regards,

    /Dirk

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Apr 23, 2010 @ 10:43
Please Sign in or register to post replies

Write your reply to:

Draft