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.
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.
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.
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.
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.
Hitting the same problem - Always returns false (using v7.1.8)
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.
Ahh yes! Thanks! - I should read the docs more carefully, before posting stuff :-)
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.
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.
is working on a reply...