Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

What is th reason that this exception shows?
It is usually caused by incorrect disoposing of SharePoint objects.
So what to do when this exception appears?
First thing to do is to look into the SharePoint log and look for something like this

Potentially excessive number of SPRequest objects (29) currently unreleased on thread 1.

There shoud be a call stack log and there you can sometimes find the function that does not correctly release the resources.

Here are some catches tha I found in our code that caused this problem
Iterating to parent web

This is BAD!

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 using(SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
  while (web.ParentWeb != null)
   //do you stuff
   web = web.ParentWeb;

This will actually dispose of the root web and not the web you actually opened.

This is also BAD!

SPList GetList(string listName)
    SPList result = null;
    SPSecurity.RunWithElevatedPrivileges(() =>{
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                result = web.Lists[listName];
    return result;

The disposing is in this case OK, but if you want to use the list outside the function. It can happen (and it will happen) that when you call the list it will try to access the already disposed web and it can also cause this exception.
There are several other reasons why this exception happens and thery are listed here:
Incorrect webtemp.xml template: here
Another problems with disposing described: here


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: