Categories
Sitecore

How to use publish to delete items in Sitecore

Stelio made a post on how to handle the deletion of items across Sitecore databases. And I remembered that I wrote code for this situation in another way.

The way that I’ve approached this is to extend the Item Delete event so I can publish deleted item by publishing the parent item.

Basically, I detect the parent of the item that is currently deleted by an editor, and I publish that parent item on each target database that we have on our Sitecore installation.

The code, with comments for this here:

using System;
using System.Collections;
using System.Collections.Generic;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Events;
using Sitecore.Publishing;
using Sitecore.SecurityModel;
namespace Sitecore.Sandbox.Handlers
{
public class AutoPublisherHandler
{
/// <summary>
/// OnItemDeleted handler.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void OnItemDeleted(object sender, EventArgs args)
{
//This handler publishes the parent of the deleted item with his descendants of a deleted item.
if (Context.IsBackgroundThread) return;
try
{
//extract the deleted item, and then extract his parent
var idParent = Event.ExtractParameter(args, 1) as ID;
var parent = Database.GetDatabase("master").GetItem(idParent);
var dataItem = parent;
//fetch the target databases
foreach (Database database in GetTargets(dataItem))
{
//publish the parrent for each database
var options = new PublishOptions(dataItem.Database, database, PublishMode.SingleItem, dataItem.Language, DateTime.Now)
{RootItem = dataItem, Deep = true};
new Publisher(options).PublishAsync();
}
}
catch (Exception ex)
{
Log.Error(ex.Message, this);
}
}
/// <summary>
/// Gets the targets.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The targets.</returns>
private IEnumerable<Database> GetTargets(Item item)
{
using (new SecurityDisabler())
{
Item targets = item.Database.Items["/sitecore/system/publishing targets"];
if (targets != null)
{
var list = new ArrayList();
foreach (Item target in targets.Children)
{
string name = target["Target database"];
if (name.Length > 0)
{
Database database = Factory.GetDatabase(name, false);
if (database != null)
{
list.Add(database);
}
else
{
Log.Warn("Unknown database in AutoPublisherHandler: " + name, this);
}
}
}
return (list.ToArray(typeof (Database)) as Database[]);
}
}
return new Database[0];
}
}
}

And the config changes needed:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="item:deleted">
<handler type="Sitecore.Sandbox.Handlers.AutoPublisherHandler, Sitecore.Sandbox" method="OnItemDeleted" />
</event>
</events>
</sitecore>
</configuration>

view raw
AutoPublisher.config
hosted with ❤ by GitHub

By Sebastian Tecsi

Sitecore MVP 2018, 2019, 2020
Sitecore Architect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.