Tuesday, August 14, 2012

How To Fix SharePoint 2010 List Item Event Receiver - Native Stack Error 0x81020089

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)
{
    properties.Status = SPEventReceiverStatus.CancelNoError;
    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);
}


Elements.xml file for the List Item Event Receiver is as follows:

 
-
-
-
  CustomItemDeleting 
  ItemDeleting 
  $SharePoint.Project.AssemblyFullName$ 
  SharePointFix.Project.ItemDeletingEvent 
  10001 
 
 
 

Receiver ListTemplateId = 100, ensures that the validation works for both Pages library and Custom SharePoint Lists.

No comments:

Post a Comment