Copied to clipboard

Flag this post as spam?

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


  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Oct 24, 2013 @ 17:26
    Jeffrey Schoemaker
    0

    Published event in V6 vs Document_AfterPublish

    Hi,

    I was trying to update some code to V6 and I ran into the triggering of events. In v4 I had the following code:

    public class AfterPublishInV4 : umbraco.BusinessLogic.ApplicationBase    
    {
    public AfterPublishInV4()       
    {     Document.AfterPublish += Document_AfterPublish;}

    void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
    {
    var newTitel = new umbraco.NodeFactory.Node(sender.Id).GetProperty("titel");
    }
    }

    And this will return the new "titel" of the node that I just published.


    When I try to do the same in V6 it will return the old "titel"

    public class AfterPublish : ApplicationEventHandler
    {
    public AfterPublish()
    {

    ContentService.Published += ContentService_Published;
    }

    void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
    {
    foreach (var node in e.PublishedEntities)
    {
    var newTitel = new umbraco.NodeFactory.Node(node.Id).GetProperty("titel")
    }
    }

    Am I doing anything wrong, is it the wrong event to trigger or do I overlook something?

    Hope somebody could help me out!

    Greetings Jeffrey

  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Oct 28, 2013 @ 08:10
    Jeffrey Schoemaker
    0

    Nobody?

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Oct 30, 2013 @ 12:59
    Morten Christensen
    100

    When using the "Published" event in v6 you will have to assume that the actually publishing (refreshing the xml cache etc) is done in the background and not necessarily finished when hitting your AfterPublish event. But at the same time its also safe to assume that the Content objects in the PublishedEntities collection corresponds to the published items (be it INode, IPublishedContent or DynamicContent). So you might as well get the title from the content you are iterating as that same content is the basis of you published content. The following will give you the same result, so no need to fetch it from the cached/published nodes:

    voidContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender,Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
           
    {
                 
    foreach(var content in e.PublishedEntities)
               
    {
                   
    var newTitel = content.GetValue<string>("title");
               
    }
    }

    Hope this helps,

    Morten

  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Oct 30, 2013 @ 17:44
    Jeffrey Schoemaker
    0

    Hi Morten,

    thanks for your reply. That was indeed the assumption I made; I must have missed it in the documentation of the V6-API ;). Now it works (although my code was a bit more complex than the above code).

    Greetings, Jeffrey

Please Sign in or register to post replies

Write your reply to:

Draft