Dan Sinclair
Dan Sinclair

Reputation: 973

How to get the parent of a Sitecore item in the item:deleted event?

When handling the item:deleted event in Sitecore, the Item that is passed in has a parent of null:

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Events;

public void OnItemDeleted(object sender, EventArgs args)
{
    Item item = Event.ExtractParameter(args, 0) as Item;
    Item itemParent = item.Parent;
    if (itemParent != null)
    {
        // Do stuff
    }
}

It never hits the // Do stuff because itemParent is always null.

Upvotes: 1

Views: 973

Answers (1)

Dan Sinclair
Dan Sinclair

Reputation: 973

The second parameter passed into this method is the Parent's ID. Update the method as follows:

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Events;

public void OnItemDeleted(object sender, EventArgs args)
{
    Item item = Event.ExtractParameter(args, 0) as Item;
    ID parentId = Event.ExtractParameter(args, 1) as ID;
    Item itemParent = item.Database.GetItem(parentId);
    if (itemParent != null)
    {
        // Do stuff
    }
}

Upvotes: 4

Related Questions