Differences between DeletingContent/DeletedContent between emptying trash and deleting content

We stumbled upon an area where the DeletingContent and DeletedContent events slightly differently when you’re deleting content through the IContentRepository, manually emptying the trash and the scheduled job “Automatic Emptying of Trash”.

Let’s look at what the DeletingContent and DeletedContent events are.

There are multiple articles and blog posts that mention this but simply these are simple event delegates when content are changed. In this case I’m interested in to do things when content is deleted. Note that moving something to the Trash in the UI is not considered as “deleting content”.

Just add an InitializationModule and hook up your events:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class InitializationModule : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var contentEvents = ServiceLocator.Current.GetInstance();
        contentEvents.DeletingContent += OnDeletingContent;
        contentEvents.DeletedContent += OnDeletedContent;
    }

    private void OnDeletedContent(object sender, DeleteContentEventArgs deleteContentEventArgs)
    {
    }

    private void OnDeletingContent(object sender, DeleteContentEventArgs deleteContentEventArgs)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

As you can see there is a class called “DeleteContentEventArgs“. This contains information for the content:

Name Summary
CancelAction Set value to abort the current event handling
CancelReason Gets or sets the reason for cancel.
Content The content that the event applies to
ContentLink The Content that the event applies to
Creator This property keeps track of the class/instance that created the page object.
DeletedDescendents Gets or sets a list of references to any descendents that will also be deleted from this delete operation.
Items Gets a key-value collection that can be used to organize and share data between events handlers during an event chain.
RequiredAccess The required access that the event applies to.
TargetLink The parent that the event applies to

Compared to most other Content Events, the DeleteContentEventArgs.Content property is null. If you’re insterested in the content you are deleting, you’ll need to use IContentLoader to get it based on DeleteContentEventArgs.ContentLink.

The special thing with DeleteContentEventArgs is that the DeleteContentEventArgs.DeletedDescendents contains ContentReferences for all content that are descendants to the content that are to be deleted.

Here is what’s different depending on how you’re deleting

IContentRepository.Delete

Let’s start with the easy thing. IContentRepository.Delete to delete your content.

var pageReference = new PageReference(133);
var contentRepository = ServiceLocator.Current.GetInstance();
contentRepository.Delete(pageReference, false);

In this case, the ContentLink is the page you’re deleting is added to DeleteContentEventArgs.ContentLink and all content beneath the content are added to the DeleteContentEventArgs.DeletedDescendents property, similar to the image above.

Scheduled Job: Automatic Emptying of Trash

This job deletes all content that was moved to the Trash more than 30 days ago.

For each child to the Trash node (usually Id 2), delete it and the DeleteContentEventArgs.ContentLink is set to that child. All content beneath that child are added to the DeletedDescendents property.

Quite straight forward, right?

Manually emptying Trash

This is where things are different.

Instead of setting DeleteContentEventArgs.ContentLink to the content that are to be deleted, the ContentReference to the Trash node is set to DeleteContentEventArgs.ContentLink.

Summary

If you’re interested in doing something when a piece of content is deleted, for example a specific Content Type, remember to check the ContentLink as well as the DeletedDescendents.

Leave a comment