Copied to clipboard

Flag this post as spam?

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


  • Laurent Lequenne 123 posts 248 karma points
    Mar 18, 2011 @ 12:13
    Laurent Lequenne
    0

    Running Action handlers through code !

    I would like to write some action handler that publishes documents, I know it was not possible before to use the handlers while publishing documents through code. But who knows ? 

    My goal is to synchronize structure of a multilanguage site... By example if some one is entering a news item in the english section, when the document is created , I would like to create an unpublished copy of the document in the other languages. Let's say now that the news document has a handler attached that creates some "date/month" structure....  I would like that the copies of the document goes through the same process, So I just need to copy the news item in the main node it belongs to, the action handler will move the document as it does with the original.

    Any suggestion is welcome !  But I guess it's more likely a change request :-)

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 18, 2011 @ 13:09
    Tom Fulton
    0

    Hi Laurent,

    You can indeed write action handlers (events), using ApplicationBase.  Here's a link to a wiki article with some examples, and also a good blog post by Richard Soeteman on events:

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events
    http://www.richardsoeteman.net/PermaLink,guid,f470b6cf-40da-4aa9-a0d9-7b984fe9bf59.aspx

    I've used them in conjunction with the Relationship API to implement a solution similar to what you describe.

    -Tom

  • Laurent Lequenne 123 posts 248 karma points
    Mar 18, 2011 @ 13:21
    Laurent Lequenne
    0

    Thank you Tom , that part I already captured:-)

    I wanted to know how can I make those action handlers work if I want to publish a new document in my code (by example in one of those handlers as here below).

     

     

     

     

     

     

    void

    Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)

    {

     

    try

     

    {

     

    new DocumentIndexer(null, null

    ).Index(sender);

     

    if

    (Blablabliblouh)

    {

    PublishSomeNewDocument that will go through the same process of the application Base

    }

    }

     

    catch

    (Exception ex)

    {

    umbraco.BusinessLogic.Log.Add(LogTypes.Error, -1,

     

     

    string.Format("DocumentIndexingHandler Document_AfterPublish exception: {0}"

    , ex.Message));

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 18, 2011 @ 13:28
    Tom Fulton
    0

    Hi,

    Sorry, just saw your other thread :)

    Are you wanting to create a new document and publish it?  Or publish an existing?  To publish an existing the .Publish method should work, see this wiki:  http://our.umbraco.org/wiki/reference/api-cheatsheet/publishing-and-republishing

    That will work to publish a document, I'm not 100% though if it will kick off an AfterPublish event for the document, but I am pretty sure it does.  If it does you might run into an infinite loop, so you might need to add some conditions before the Publish is called.  In one case I set a session variable to handle this

    If you want to create a brand new document and publish it you can use Document.MakeNew to create it (see http://our.umbraco.org/wiki/reference/api-cheatsheet/creating-a-document), or you may even want to copy the initial document in which case you can use .Copy

    -Tom

  • Laurent Lequenne 123 posts 248 karma points
    Mar 18, 2011 @ 14:29
    Laurent Lequenne
    0

    Oki, I just figured out while testing my action handler... I know that was not working with earlier functions of umbraco with eventhandlers or I don't know how they call this :-) ... But I didn't tried since then !  :-)

     

    Thank you.

Please Sign in or register to post replies

Write your reply to:

Draft