SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I think the second line should not use using because its in the sp context.

using (SPSite SiteCol = new SPSite(SPContext.Current.Web.Url))
                    using (SPWeb UpdateCurrentweb = SiteCol.RootWeb)
share|improve this question
up vote 3 down vote accepted

No it's not good coding practice, but the issue isn't wrong disposal, but maybe wrong web and bad performance

The code allocated a completely new SPSite and new SPWeb, the only link to SPContext is that the new SPSite is pointing to the same as SPContext.Current.Site, but it's a new object.

Wrong web
Based on the naming of the new SPWeb the programmer might have thought that the SPWeb pointing to the same as SPContext.Current.Web, but that's only the case if SPContext.Current.Web is the RootWeb.

Why make all the database calls to get a new SPSite and SPWeb???
Depending on which site your need you should just use SPContext.Current.Web or SPContext.Site.RootWeb

share|improve this answer

No explicit dispose required for RootWeb.

The SPSite you create is automatically disposed by implementing using(). The RootWeb will be disposed by SPSite. So you can write :

using (SPSite SiteCol = new SPSite(SPContext.Current.Web.Url))
                    SPWeb UpdateCurrentweb = SiteCol.RootWeb;

                    // Use RootWeb
share|improve this answer

Actually, your first using row creates a new SPSite istance that (as far as I know) has no memory whatsoever of having begin constructed from a url taken from the SPContext - so the .RootWeb instance should be a different instance from the one in the context.

Anyway, the "latest" Sharepoint Dispose/Do Not Dispose guidance reports that the .RootWeb property of an SPSite object no longer needs explicit disposal, so I would just skip that. For further reference you can look here and here on Roger blog, or here on msdn.

// New SPSite.
using (SPSite siteCollection = new SPSite("http://moss"))
    SPWeb rootWeb1 = siteCollection.RootWeb;
    // No explicit rootWeb1 dispose required.
}  // siteCollection automatically disposed by implementing using().
// rootWeb1 will be Disposed by SPSite.

Notice that the linked documentation from Roger Lamb also reports that

For existing SharePoint customizations removal of explicit RootWeb Dispose is recommended to avoid an edge case condition where the SPContext.Current.Web has equality to the SPSite.RootWeb.

I suppose that equality should be read as "same instance", so that shouldn't be your case,but since you don't need to dispose the .RootWeb instance anyway... better safe than sorry.

share|improve this answer

If the second line was as shown below

using (SPWeb currentWeb = SPContext.Current.Web)

Then, it should have been corrected to

using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
        using (SPWeb web = siteCollection.OpenWeb())

In short, SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. If you dispose SPContext objects in your code, it will no longer be available for any other pending code.

Visit the msdn article about disposing SharePoint objects for more details and also a similar post from SPSE where you could see different approaches while coding.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.