在Sitecore中处理item:deleted
事件时,传入的Item
的父项为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
}
}
它从未到达// Do stuff
,因为itemParent
始终为空。
答案 0 :(得分:4)
传递给此方法的第二个参数是Parent的ID。更新方法如下:
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
}
}