When creating List Item Event Receivers, we added the ItemDeleting event to prevent List Items from being deleted:
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.ErrorMessage = "Bugs can only be resolved not deleted!";
//You may also redirect to a custom error URL page:
//SPUtility.Redirect(web.Url + Constants.ERROR_PAGE_URL + errorMessage, SPRedirectFlags.DoNotEndResponse, HttpContext.Current);
}
You would think that the above piece of code will show an appropriate validation message while deleting a list item, but instead it throws a native call stack error.
We identified that these code lines do not work very well with List Item event receivers:
properties.Cancel = true; and
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
As a result we see a Native Stack Error Message:
We modified the ItemDeleting Event receiver code and it started working fine:
public override void ItemDeleting(SPItemEventProperties properties)
{
Elements.xml file for the List Item Event Receiver is as follows:
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.ErrorMessage = "Bugs can only be resolved not deleted!";
//You may also redirect to a custom error URL page:
//SPUtility.Redirect(web.Url + Constants.ERROR_PAGE_URL + errorMessage, SPRedirectFlags.DoNotEndResponse, HttpContext.Current);
}
You would think that the above piece of code will show an appropriate validation message while deleting a list item, but instead it throws a native call stack error.
We identified that these code lines do not work very well with List Item event receivers:
properties.Cancel = true; and
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
As a result we see a Native Stack Error Message:
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Status = SPEventReceiverStatus.CancelNoError;
properties.ErrorMessage = "Bugs can only be resolved not deleted!";
//You may also redirect to a custom error URL page:
//You may also redirect to a custom error URL page:
//SPUtility.Redirect(web.Url + Constants.ERROR_PAGE_URL + errorMessage, SPRedirectFlags.DoNotEndResponse, HttpContext.Current);
}
Elements.xml file for the List Item Event Receiver is as follows:
<?xml version="1.0" encoding="utf-8" ?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <Receivers ListTemplateId="100">
- <Receiver>
<Name>CustomItemDeleting</Name>
<Type>ItemDeleting</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>SharePointFix.Project.ItemDeletingEvent</Class>
<SequenceNumber>10001</SequenceNumber>
</Receiver>
</Receivers>
</Elements>
Receiver ListTemplateId = 100, ensures that the validation works for both Pages library and Custom SharePoint Lists.