Using the SharePoint 2010 RC I am having issues canceling the deletion of a list item using event receivers. My code is firing, setting the cancel property of the SPItemEventProperties, setting an error message, and throwing an error back to the calling thread. This approach works fine in the adding/updating methods, however, when used in the deleting method I can watch the code fire in a debugger, but the item is still moved to the site's recycling bin.
Further, I am seeing this behavior in a site created from the "CMSPUBLISHINGSITE#2" template from stsadm, but not from a site created from the "Team Site" template via Central Administration.
The misbehaving code follows:
public override void ItemDeleting(SPItemEventProperties properties)
{
if (!(properties.UserLoginName == "SHAREPOINT\\system"))
{
try
{
throw new CreatorIdAliasingException("Please contact support if you feel a release web site has been inappropriately assigned to your organization.");
}
catch (CreatorIdAliasingException ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.ToString();
properties.InvalidateListItem();
throw;
}
}
}
For reference, identical code is contained in the ItemAdding method and works as expected.
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if (!(properties.UserLoginName == "SHAREPOINT\\system"))
{
try
{
throw new InvalidCreatorIdException("Please contact support to add a known URL to your list of release web sites.");
}
catch (InvalidCreatorIdException ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.ToString();
properties.InvalidateListItem();
throw;
}
}
}