Skip to main content

Cloud

DISP_E_EXCEPTION and the Root Web….

Last week we came across a very interesting problem. A routine deployment made some of the web parts throw an exception whenever you made a change to its properties and tried to save the changes. This exception was also seen when we tried to add a web part to a page. It was not only for custom web parts but also for OOTB (Out of the box) web parts.

Now what could have possibly happen that would make the OOTB web parts break and throw an exception. Looking into the issue, I found that the culprit was actually the disposable of the SPWeb objects in one of the custom web parts. And since these web parts were on the master page, the exceptions occurred irrespective of the site that user was on.

Here is the code that caused the exception:

   1: SPSite site = SPContext.Current.Site;
   2: SPWeb rootWeb = site.RootWeb;
   3: ...
   4: rootWeb.Dispose();

There is specific condition when you are supposed to dispose it and when you are supposed to leave it alone. The condition is that when the contextual site is the root site then the RootWeb object should not be disposed. As disposing this object implies that the current root web object which is getting shared has been disposed of and thus the web parts which access this property will not get the Root Web SPWeb object and will throw an exception.

So the code needed the following change:

   1: if (!SPContext.Current.Web.IsRootWeb)
   2:     rootWeb.Dispose();

Checking if the current web is the root web and if it is not then only disposing the root web object.

This did the trick. I have read that this happens only when you are on the root site. But in our case it was happening irrespective of the site that the user was on. I believe that this was because the problematic web parts were on the master page.

Share this post :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Amol Ajgaonkar

More from this Author

Follow Us