The ContentService class is the most commonly used type when extending Umbraco using events. ContentService implements IContentService. It provides easy access to operations involving IContent.
Example usage of the ContentService events:
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
namespace My.Namespace
{
public class MyEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentServicePublished;
}
private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
{
foreach (var node in args.PublishedEntities)
{
if (node.ContentType.Alias == "Comment")
{
SendMail(node);
}
}
}
}
}
Event | Signature | Description |
---|---|---|
Saving | (IContentService sender, SaveEventArgs<IContent> e) |
Raised when ContentService.Save is called in the API. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IContentService object. "e" will provide: NOTE: If the entity is brand new then HasIdentity will equal false.
|
Saved | (IContentService sender, SaveEventArgs<IContent> e) |
Raised when ContentService.Save is called in the API and after data has been persisted. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IContentService object. "e" will provide: NOTE: See here on how to determine if the entity is brand new
|
Publishing | (IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e) |
Raised when ContentService.Publishing is called in the API. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Publish method call (true by default). "sender" will be the current IPublishingStrategy object. "e" will provide: NOTE: If the entity is brand new then HasIdentity will equal false.
|
Published | (IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e) |
Raised when ContentService.Publish is called in the API and after data has been published. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Publish method call (true by default). "sender" will be the current IPublishingStrategy object. "e" will provide: NOTE: See here on how to determine if the entity is brand new
|
Copying | (IContentService sender, CopyEventArgs<IContent> e) |
Raised when ContentService.Copy is called in the API. The event is fired after a copy object has been created and had its parentId updated and its state has been set to unpublished. "sender" will be the current IContentService object. "e" will provide:
|
Copied | (IContentService sender, CopyEventArgs<IContent> e) |
Raised when ContentService.Copy is called in the API. The event is fired after the content object has been copied. "sender" will be the current IContentService object. "e" will provide:
|
Moving | (IContentService sender, MoveEventArgs<IContent> e) |
Raised when ContentService.Move is called in the API. NOTE: If the target parent is the Recycle bin, this event is never fired. Try the Trashing event instead. "sender" will be the current IContentService object. "e" will provide:
|
Moved | (IContentService sender, MoveEventArgs<IContent> e) |
Raised when ContentService.Move is called in the API. The event is fired after the content object has been moved. NOTE: If the target parent is the Recycle bin, this event is never fired. Try the Trashed event instead. "sender" will be the current IContentService object. "e" will provide:
|
Trashing | (IContentService sender, MoveEventArgs<IContent> e) |
Raised when ContentService.MoveToRecycleBin is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
Trashed | (IContentService sender, MoveEventArgs<IContent> e) |
Raised when ContentService.MoveToRecycleBin is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
Deleting | (IContentService sender, DeleteEventArgs<IContent> e) |
Raised when ContentService.DeleteContentOfType, ContentService.Delete, ContentService.EmptyRecycleBin are called in the API. "sender" will be the current IContentService object. "e" will provide:
|
Deleted | (IContentService sender, DeleteEventArgs<IContent> e) |
Raised when ContentService.Delete, ContentService.EmptyRecycleBin are called in the API. "sender" will be the current IContentService object. "e" will provide:
|
DeletingVersions | (IContentService sender, DeleteRevisionsEventArgs e) |
Raised when ContentService.DeleteVersion, ContentService.DeleteVersions are called in the API. "sender" will be the current IContentService object. "e" will provide:
|
DeletedVersions | (IContentService sender, DeleteRevisionsEventArgs e) |
Raised when ContentService.DeleteVersion, ContentService.DeleteVersions are called in the API. "sender" will be the current IContentService object. "e" will provide:
|
RollingBack | (IContentService sender, RollbackEventArgs<IContent> e) |
Raised when ContentService.Rollback is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
RolledBack | (IContentService sender, RollbackEventArgs<IContent> e) |
Raised when ContentService.Rollback is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
SendingToPublish | (IContentService sender, SendToPublishEventArgs<IContent> e) |
Raised when ContentService.SendToPublication is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
SentToPublish | (IContentService sender, SendToPublishEventArgs<IContent> e) |
Raised when ContentService.SendToPublication is called in the API. "sender" will be the current IContentService object. "e" will provide:
|
Both the ContentService.Creating and ContentService.Created events have been obsoleted. Why? Because these events are not guaranteed to trigger and therefore should not be used. This is because these events only trigger when the ContentService.CreateContent method is used which is an entirely optional way to create content entities. It is also possible to simply construct a new content item - which is generally the preferred and consistent way - and therefore the Creating/Created events will not execute when constructing content that way. Further more, there's no reason to listen for the Creating/Created events because they are misleading since they don't actually trigger before and after the entity has been persisted, they simply trigger inside the CreateContent method which never actually persists the entity, it simply just constructs a new content object.
The ContentService.Saving and ContentService.Saved events will always trigger before and after an entity has been persisted. You can determine if an entity is brand new in either of those events. 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