Do you know to never dispose objects from SPContext.Current?

Last updated by Brady Stroud [SSW] 8 months ago.See history

Disposing objects in SharePoint is important, but never do it with objects from SPContext.Current. SharePoint will manage disposing these objects itself.

using (SPWeb web =  SPContext.Current.Site.RootWeb)
{
    //do something here
}

Figure: Using statement is trying to dispose current site object - it will cause exception

Just simply use "Current" object directly.

SPWeb webSPContext.Current.Site.RootWeb;
//do something here

Figure: Use Current objects directly - don't need to dispose them


William Yin
Brendan Richards
We open source.Loving SSW Rules? Star us on GitHub. Star
Stand by... we're migrating this site to TinaCMS