Navigation

Tuesday 4 March 2008

Correctly disposing SPWeb objects

I guess SharePoint development is a bit like riding a wave. First off you find you do almost everything wrong ... after a while you pick up some of the "best practices" (like disposing SPWeb objects) and then you find that you are still doing things wrong. Well ... thats the learning curve I'm afraid .. and it rounds down to where your SPWeb object came from, and when you should be disposing it.
There are several different ways of retrieving an SPWeb object.
Take the following 3 examples:
1) SPContext
SPWeb web = SPContext.Current.Web
2) OpenWeb
SPSite site = new Site("http://myserver/");
SPWeb web = site.OpenWeb("");
3) Site "RootWeb"
SPSite site = new Site("http://myserver/");
SPWeb web = site.RootWeb;
All 3 of them are valid SPWeb objects, but they are also all slightly different.
Lets start off with SPContext. This is basically a context object which represents the context of the current site that you are navigating. The SPContext.Current reflects where you currently are in the SharePoint site collection (so SPContext.Current.Web will return the SPWeb object that represents the site from which your code is executing ... for example .. the site that your web part is sat in).
There are 2 major issues with using SPContext.
1) It is context sensitive. Yes .. I know thats like saying the sky is blue, but you do need to consider this if you are looking at generic code that might be accessed from multiple projects. Pulling out the "SPContext" properties is fine from a web part, but you don't want the same code executing from a Console Application or a Workflow Custom Action.
2) SPContext is in use by all manor of controls and parts on the site and the page. If you dispose of the SPContext.Current.Web object then you will find that you get a nasty error message next time you try to access that object (forcing you to refresh the page). So if you've ever seen the error message below, check your disposal!
"Trying to use an SPWeb object that has been closed or disposed and is no longer valid"
Make sure you dispose of OpenWeb("") objects!
This is very important. When you are basically just creating your own SPWeb objects you will need to make sure that you dispose of your objects correctly. The best way of doing this is encompassing your web object in a "Using" statement
using(SPSite site = new Site(http://myserver/);
{
using(SPWeb web = site.OpenWeb(""))
{
// work with web object
} // web object disposed
} // site object disposed
Do NOT Dispose SPContext.Current.Web or SPSite.RootWeb
If you write a using statement such as:
// This is bad! m'kay ?
using(SPWeb web = SPContext.Current.Web)
{
}
What you are actually doing is disposing the SPContext.Current.Web object .. not just your own "web" object. THIS IS BAD. You should actually just declare them with a single, normal line, such as:
SPWeb web = SPContext.Current.Web;
And exactly the same applies to SPSite.RootWeb properties too!