Copied to clipboard

Flag this post as spam?

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


  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 12:37
    Calvin Frei
    0

    How to get currentNode in the PublishEventHandler

    Hi Umbracians

    How I can get the currentNode in the PublishEventHandler? I have tried

    Node currentNode = new Node(sender.Id);

    but this don't work for me...

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Feb 17, 2010 @ 12:47
    Thomas Höhler
    1

    The first parameter of the PublishEventHandler is the actual Document on which this event is performed.

    Take a look into the wiki: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events

    hth, Thomas

  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 13:09
    Calvin Frei
    0

    Thanks for your reply Thomas, but this is not my problem ;-)...

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Feb 17, 2010 @ 13:14
    Ismail Mayat
    1

    Calvin,

    Take a look at http://our.umbraco.org/wiki/reference/api-cheatsheet/difference-between-a-node-and-a-document in action handler you use the document becuase you already have it passed to you.

    Regards

     

    Ismail

  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 13:54
    Calvin Frei
    0

    We would like to offer a link to the frontend (something like node.NiceUrl) in the event handler.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Feb 17, 2010 @ 14:08
    Ismail Mayat
    0

    Calvin,

    Can you explain the full context of your scenario, it may be that what you are trying to do does not need to be done in action handler but xslt and template?

    Regards

    Ismail

  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 14:25
    Calvin Frei
    0

    Okay sorry for my small descripton :-).

    We try to send with an event handler a mail to an user when he has a new entry in his node. The user gets the mail, but the NiceUrl (e.g. www.domain.ch/products.aspx) is missing. Here a short part of the code.

    ...
    Document currentDoc = new Document(sender.Id);
    
    if (currentDoc != null) {
      Node parentCmsNode = new Node(currentDoc.Parent.Id);
    
      if (parentCmsNode != null) {
         StringBuilder sb = new StringBuilder();
         sb.AppendLine("Live-Url: www.domain.ch" + ???);
         ...
         }
    

    We have tried

     currentDoc.getProperty("umbracoUrlName").Value
    

    and

     currentNode.NiceUrl
    

    and more...

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 17, 2010 @ 15:53
    Dirk De Grave
    0

    Hi,

    NiceUrl will give you the path to the node

    var url = umbraco.library:NiceUrl(currentNode.Id);

    (Be aware tho that this might be a relative path if useDomainPrefix is set to false (umbracoSettigns.config) - Changing this to "true" will get you the complete path in combination if you've also set the domain name on the top level node using 'Manage hostnames')

     

    Hope this helps.

    Cheers,

    /Dirk

     

     

  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 16:02
    Calvin Frei
    0

    Thanks Dirk for your solution, but

    sb.AppendLine("Live-URL: http://www.domain.ch" + umbraco.library.NiceUrl(currentDoc.Id));
    

    don't work :-/.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Feb 17, 2010 @ 16:43
    Ismail Mayat
    2

    Calvin,

    If at the time of event the document is not published then niceurl will not work, also you dont need todo the extra new document call you already have the document namely sender.  Think there is post publish event do the niceurl there becuase then doc is published. One thing when you say dont work do you get error?

    Regards

    Ismail

  • Calvin Frei 106 posts 314 karma points
    Feb 17, 2010 @ 16:52
    Calvin Frei
    0

    We already firing with Document.AfterPublish. No we don't get an error only an empty string.

    Calvin

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Feb 17, 2010 @ 18:16
    Morten Christensen
    0

    Hi Calvin,

    if you want to send an email with the link after a node has been published then this should work (just tried out to be sure):

    using System.Text;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;

    namespace Application
    {
    public class DocumentEvent : ApplicationBase
    {
    public DocumentEvent()
    {
    Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
    }

    void Document_AfterPublish(Document sender, PublishEventArgs e)
    {
    string nodeUrl = umbraco.library.NiceUrl(sender.Id);
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Live-Url: www.domain.ch" + nodeUrl);

    }
    }
    }

    AfterPublish you will be able to use the id of the node (published document) to get the Url with this line: umbraco.library.NiceUrl(sender.Id)

    - Morten

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Feb 17, 2010 @ 22:29
    Aaron Powell
    0

    Why don't you just use the built in notification system? That has quite a few different events which you can subscribe to, including publish, which will send an email

  • Calvin Frei 106 posts 314 karma points
    Feb 18, 2010 @ 08:58
    Calvin Frei
    0

    @ Morten

    Thank you for your solution, but i become the Url (e.g. www.domain.ch/products.aspx) as recently as I publish the node twice.

    @ slace

    We do not want to create backend users...

  • Tobias Morf 80 posts 183 karma points
    Feb 23, 2010 @ 09:39
    Tobias Morf
    0

    We are still trying to get the “Nice URL” of a new created node in the Document_AfterPublish event. It’s no problem no catch it when you push the “Publish button” a second time. But the first time the result is just an empty string (umbraco.library.NiceUrl(sender.Id) == String.Empty).

    Any ideas?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 23, 2010 @ 10:09
    Lee Kelleher
    101

    Hi Calvin and Tobias,

    As far as I can tell, (from my limited knowledge of the core code), the filesystem XML cache (in /data/umbraco.config) is not updated with the Document.Publish method (it happens elsewhere - not sure where - but it doesn't happen here).

    So, since NiceUrl uses the XML cache (via the filesystem), you can't get the URL in the AfterPublish event.

    What you need to do is manually update the XML document cache like this:

    void Document_AfterPublish(Document sender, PublishEventArgs e)
    {
        umbraco.library.UpdateDocumentCache(sender.Id);
    
        String nodeUrl = umbraco.library.NiceUrl(sender.Id);
    
        ...
    }

    Good luck, Lee.

  • Calvin Frei 106 posts 314 karma points
    Feb 24, 2010 @ 11:36
    Calvin Frei
    0

    Hi Lee your solution is perfect and get the desired effect! Thank you a lot!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 24, 2010 @ 12:12
    Lee Kelleher
    0

    Hi Calvin, glad that it worked! :-)

    I looked through the source (via Reflector) to see where the UpdateDocumentCache is called and it's by the Publish on the editContent control (ref: "umbraco.cms.presentation.editContent.Publish").

    So using it in your Event handler will be calling it twice ... but I guess that's a small overhead for your desired result! So it's worth it!

    Cheers, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 24, 2010 @ 12:18
    Lee Kelleher
    4

    Just thought of a better solution... hook into the "AfterUpdateDocumentCache" event instead!

    This happens after the XML cache is regenerated. It's also where the old ActionPublish handler is... which makes so much more sense!

    umbraco.content.AfterUpdateDocumentCache

    I guess the naming convention of "Document.AfterPublish" makes developers go straight for that, whereas "AfterUpdateDocumentCache" isn't so obvious!

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft