SharePoint annoyance 03 – Irrelevant error when you try to cancel the checking out on your ItemCheckingOut event receiver


When attempting to cancel an item checkout in the ItemCheckingOut event handler the error “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.” is shown to the user. This is a known issue with WSS 3.0 however, there is a workaround. You should set the Status property to SPEventReceiverStatus.CancelNoError instead of SPEventReceiverStatus.CancelWithError or setting properties.Cancel = true;

public override void ItemCheckingOut(SPItemEventProperites properties)
{
     // your code to check whether or not the current user can do a checkout on the item
     properties.Status = SPEventReceiverStatus.CancelNoError;
}

However, doing so will not display an error message and in case of a publishing page, SharePoint will think that the page is checked-out and display (un)relevant error when the user will try to checked-in or publish the page. (“page must be first checked-out”)

My solution was instead, to transfer to an error page using the SPUtility.TransferToErrorPage(“You’re not allowed to check-out this kind of file”); to avoid any unexpected behaviour and ensure that the flow is stopped but that didn’t work. Like an asynchroneous operation no error was returned and the usual (wrong) behavior was still there.

So I created a custom error page (in the _layouts folder) that will display any message received as a querystring parameter to fully replace the      properties.Status = SPEventReceiverStatus.CancelNoError;

public override void ItemCheckingOut(SPItemEventProperites properties)
{
     // your code to check whether or not the current user can do a checkout on the item
   if (currentUserIsNotAllowedToDoCheckOut)
  {
        string errorPage = “/_layouts/BlueOcean/ErrorPages/GenericError.aspx”;
        string errorMessageQueryString = “message=” + HttpUtility.UrlEncode(errorMessage);
        SPUtility.Redirect(errorPage, SPRedirectFlags.Default, currentContext, errorMessageQueryString);
  }
}

In order to have the context in the event don’t forget to do this

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s