Tuesday, May 1, 2012

Error: "The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request"

I created a Sandboxed Solution on SharePoint 2010 including one web part and three event receivers registered to one list's ItemAdded, ItemDeleting and ItemUpdated. The web part has a delete button used to delete the list's items.

I deployed the sandboxed solution package to the site collection, when I click delete button on the web part, the error message: "The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request"

I googled this message, most of the articles suggest to check if the Microsoft SharePoint Foundation Sandboxed Code Service is running on Central Admnistration and other settings on the system, the approaches didn't work for me.

I put one web part and three event receivers to seperate features, I found if I deactivate the ItemDeleting event receiver feature, the web part works well without any problem. Obviously the web part deleting fires the ItemDeleting event receiver.

So I need to disable the ItemDeleting event firing from the web part deleting code. I created one column on the list as a flag, it is set in the web part code. if this flag is found in the ItemDeleting event, just ignore the event.

I deployed and tested the project on my dev machine, the error message is gone, it's working well. When I deployed it to another SharePoint server, the same error message was back.

Then I google the following solution

//Code in delete button of the web part.
using (new DisabledItemEventsScope())
{
 //Code to delete the items.
}

public class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
        private bool eventFiringEnabledStatus;
        public DisabledItemEventsScope()
        {
            eventFiringEnabledStatus = base.EventFiringEnabled;
            base.EventFiringEnabled = false;
        }
        #region IDisposable Members
        public void Dispose()
        {
            base.EventFiringEnabled = eventFiringEnabledStatus;
        }
        #endregion
}

I applied this code to my project and deployed the project, the error message is gone and everything is working well.