Determining if an entity is new
Many of the Umbraco services expose a 'Saved' event (or similar). In some cases it is beneficial to know if this entity is a brand new entity that has been persisted to the database. This is how you can determine this.
Are you using Umbraco 9?
Note that in Umbraco 9, Events have been renamed to Notifications.
Find more information about notifications in Umbraco 9 in the Notifications section.
Checking if it's new
We know that if an entity is new and hasn't been persisted that it will not have an ID. Therefore we know if an entity has been newly persisted to the database by checking if its ID was changed before being persisted.
Here's the snippet of code that does that:
foreach (var entity in e.SavedEntities)
{
var dirty = (IRememberBeingDirty)entity;
var isNew = dirty.WasPropertyDirty("Id");
}
To check if an entity is new in the ContentService.Saving event, use the following:
foreach (var entity in e.SavedEntities)
{
var isNew = entity.HasIdentity == false;
}
How it works
This is all possible because of the IRememberBeingDirty
interface. Indeed the name of this interface is hilarious but it describes exactly what it does. All entities implement this interface which is extremely handy. It tracks not only the property data that has changed (because it inherits from yet another hilarious interface called ICanBeDirty
) but also the property data that was changed before it was committed.