Copied to clipboard

Flag this post as spam?

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


  • Yiannis Vavouranakis 38 posts 78 karma points
    Jul 04, 2013 @ 16:09
    Yiannis Vavouranakis
    0

    API for finding a newly created node?

    In v4, we could subscribe to the Document.AfterNew event, and get a reference to a newly created document. Then we could do all sorts of things with it.

    In v6, the AfterNew does not fire anymore. We tried going with ContentService.Created event, but the entity in the event argument has not been persisted to the database yet, therefore it does not have an Id (which is necessary for most operations regarding a content item).  Then we went to ContentService.Saved event, where the entities (!?) in the event argument all have properly set Id's etc. However, we can't find a way to distinguish between newly created content and content that has been created before and it has just been saved. Let me elaborate:

    1. While debugging, we noticed that for a new item, CreateDate is not equal to UpdateDate (UpdateDate is slightly later). So we can't use that as a criterion

    2. Status does not have a status for newly created items

    3. IsValid cannot be accurately used, as the new item may be valid already (e.g. have the StandardValues package pre-fill all required properties).

    4. HasPublishedVersions is not reliable, as a new item and an old, never-published item will both have this to false.

    5. Number of  versions is not reliable, as creating two items without any other action in between results in both of them having 1 version.

    So then, how do we distinguish between new items that have just been created and others?

    Regards,
    Yiangos

  • Michael Wulff Nielsen 11 posts 74 karma points
    Jul 04, 2013 @ 16:22
    Michael Wulff Nielsen
    0

    Just random thoughts on this:

    1. Since the saved event fires immediatly af persistance to the db, check if DateTime.Now and Entity.Created are within 10 seconds of each other. This is an ugly hack.

    2. I haven't tested this but if Umbraco passes the same entity to both events you should be able to store a reference to the object in Created event and when you see it later in Saving take the proper action.

    Both solutions are ugly, but they might work.

  • Yiannis Vavouranakis 38 posts 78 karma points
    Jul 04, 2013 @ 16:41
    Yiannis Vavouranakis
    0

    Thanks for the reply.

    I briefly considered 1, but it really feels dirty. I'll try 2 and let you know (it also feels dirty, but somewhat less so).

    Yiangos

  • Yiannis Vavouranakis 38 posts 78 karma points
    Jul 04, 2013 @ 17:02
    Yiannis Vavouranakis
    0

    I tried the second suggestion, and it is working (though it feels hacky).

    My initial issue is not really gone, but I'm one step closer now.

    To clarify, my initial issue is that I want each new node to go at the top of its siblings, not the bottom. It seems that the bottleneck occurs because ContentService saves all siblings, as they all change their sortorder when the last node moves to the top. Indeed, measuring the time it takes to sort the items when the last is moved to the top seems to back my assumption that the bottleneck is in code outside my reach.

     

    Ah, well, at least I've managed to limit the code within my ContentService.Saved event handler so that it runs only once, just for the newly created item.

     

     

  • Yiannis Vavouranakis 38 posts 78 karma points
    Jul 25, 2013 @ 09:30
    Yiannis Vavouranakis
    0

    To continue on this, it seems that 6.1.3 will be fixing this issue. The Created event supposedly will return the new Content ID. We'll see...

Please Sign in or register to post replies

Write your reply to:

Draft