Sunday, July 31, 2011

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:

<?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.

7 comments:

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

    This solved a crucial issue for me. Thanks a lot.

    ReplyDelete
  2. Doesn't work at all for me (SPPS 2010)...
    My code:

    if (errorCondition)
    {
    properties.Status = SPEventReceiverStatus.CancelWithError ;
    properties.ErrorMessage = "My error text!";
    properties.Cancel = true;
    }

    Still getting page with this COMException and my "My error text!" added there. But I the error page ONLY with my text.

    ReplyDelete
  3. You need to try this condition:

    if (errorCondition)
    {
    properties.Status = SPEventReceiverStatus.CancelNoError;
    properties.ErrorMessage = "Your Error Text here";
    }

    ReplyDelete
  4. Now it rejects the row silently, i.e. without any error page. But this is not what I need in this case.

    ReplyDelete
  5. Try this approach:

    if (errorCondition)
    {
    properties.Cancel = true;
    properties.Status = SPEventReceiverStatus.CancelNoError;
    properties.ErrorMessage = "Your Error Text here";
    }

    Do you now get a Modal Dialog error pop-up? Also ensure you have the latest service pack installed for SP 2010.

    ReplyDelete
  6. Nope, doesn't help at all, although SP1 for Sharepoint 2010 was installed.

    ReplyDelete
  7. Hey Senglory, did it get resolved for you, am getting the same problem.
    With
    properties.Status = SPEventReceiverStatus.CancelNoError; it rejects the row silently
    and with
    properties.Status = SPEventReceiverStatus.CancelWithError ;
    it gives the error page with my custom msg.
    Any ideas?

    ReplyDelete