Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Oct 01, 2014 @ 15:44
    suzyb
    0

    Checking for new entity in published event

    I've created a published event in which I check IsNewEntity() to see if the entity is new or not.  The method always returns false however even when is it a new document.

    Am I misunderstanding how IsNewEntity() works.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Oct 29, 2014 @ 14:34
    Paul Wright (suedeapple)
    0

    Hitting the same problem - Always returns false (using v7.1.8)

         void ContentService_Saving(IContentService sender, Umbraco.Core.Events.SaveEventArgs e)
    {
    foreach (var item in e.SavedEntities)
    {
    bool isNew = item.IsNewEntity();

    if (isNew)
    {
    //do stuff
    }
    }

    }
  • Dan Lister 416 posts 1974 karma points c-trib
    Oct 29, 2014 @ 14:42
    Dan Lister
    0

    Hi Suzyb and Paul,

    In the Saving event - before the entity is persisted - you can check the entity's HasIdentity property which will be 'false' if it is brand new. In the Saved event you can use this extension method. Take a look at the last paragraph on this page from more information.

    Thanks, Dan.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Oct 29, 2014 @ 14:44
    Paul Wright (suedeapple)
    0

    Ahh yes! Thanks! - I should read the docs more carefully, before posting stuff :-)

  • suzyb 474 posts 932 karma points
    Oct 29, 2014 @ 14:56
    suzyb
    0

    I'm using the Published event as shown in the example code in the documentation.  Although I see there is no mention of this event in the events list, is it not an "official" event which is why IsNewEntity() doesn't work.

  • Dan Lister 416 posts 1974 karma points c-trib
    Oct 29, 2014 @ 15:58
    Dan Lister
    0

    That's a good point. I wonder if it has been missed out as I'm pretty certain the Published event is an official event.

    The problem you may face with checking in the Published event is that a new page will have already been saved before it reaches that event. You might be best off checking in the Saved event instead as I'm not sure if it will be marked as new in the Published event using the method I described above.

    Thanks, Dan.

Please Sign in or register to post replies

Write your reply to:

Draft