Skip to content

Latest commit

 

History

History
300 lines (286 loc) · 13.2 KB

ContentService-Events.md

File metadata and controls

300 lines (286 loc) · 13.2 KB

#ContentService Events#

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.

Usage

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);
                }
            }
        }
    }
}

Events

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.
  1. SavedEntities: Gets the collection of IContent objects being saved.
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
  1. SavedEntities: Gets the saved collection of IContent objects.
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.
  1. PublishedEntities: Gets the collection of IContent objects being published.
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
  1. PublishedEntities: Gets the published collection of IContent objects.
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:
  1. Copy: Gets the IContent object being copied.
  2. Original: Gets the original IContent object.
  3. ParentId: Gets the Id of the parent of the IContent being copied.
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:
  1. Copy: Gets the copied IContent object.
  2. Original: Gets the original IContent object.
  3. ParentId: Gets the Id of the parent of the IContent being copied.
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:
  1. Entity: Gets the IContent object being moved.
  2. ParentId: Gets the Id of the parent of the IContent being moved.
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:
  1. Entity: Gets the moved IContent object.
  2. ParentId: Gets the Id of the parent of the IContent moved.
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:
  1. Entity: Gets the IContent object being trashed.
  2. ParentId: Gets the Id of the RecycleBin.
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:
  1. Entity: Gets the trashed IContent object.
  2. ParentId: Gets the Id of the RecycleBin.
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:
  1. DeletedEntities: Gets the collection of IContent objects being deleted.
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:
  1. DeletedEntities: Gets the collection of deleted IContent objects.
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:
  1. Id: Gets the id of the IContent object being deleted.
  2. DateToRetain: Gets the latest version date.
  3. SpecificVersionId: Gets the id of the IContent object version being deleted.
  4. IsDeletingSpecificRevision: Returns true if we are deleting a specific version.
  5. DeletePriorVersions: False by default.
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:
  1. Id: Gets the id of the deleted IContent object.
  2. DateToRetain: Gets the latest version date.
  3. SpecificVersionId: Gets the id of the deleted IContent version.
  4. IsDeletingSpecificRevision: Returns true if we are deleting a specific version.
  5. DeletePriorVersions: False by default.
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:
  1. Entity: Gets the IContent object being rolledback.
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:
  1. Entity: Gets the rolledback IContent object.
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:
  1. Entity: Gets the IContent object being sent to publish.
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:
  1. Entity: Gets the sent IContent object to publish.

What happened to Creating and Created events?

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.

What do we use instead?

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