6

I'm hoping someone can help me out. I need to get the root web of the current site from the SPContext. It's easily done with the following

SPContext.Current.Site.RootWeb

I'm comfortable with the idea that the SPSite object here at SPContext.Current.Site.RootWeb shouldn't be disposed of, but what about the SPWeb object I'm getting from the SPSite. Will, when the SPSite get's disposed, the rootweb SPWeb get disposed to? Or do I need to dispose of it myself?

Mark
  • 487
  • 1
  • 7
  • 20
  • Here's a great MSDN article that shows examples and counter-examples what should and should not be disposed (RootWeb should never be explicitly disposed): http://blogs.msdn.com/b/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_2304/updated-spsite-rootweb-dispose-guidance.aspx – Paweł Bulwan Oct 09 '14 at 13:39

3 Answers3

6

Calls to SPSite.RootWeb should not be disposed. Disposing the SPSite will also dispose the RootWeb.

There is a bug in SPDisposeCheck where it flags if you do dispose it, and if you don't (damned either way!) I detailed how I solved this in this blog post, as you can't use an SPDisposeCheckIgnore attribute in elevated privileges blocks.

Andy Burns
  • 917
  • 4
  • 8
  • Yeah I noticed the problem with SPDisposeCheck giving you errors either if you dispose or don't dispose of rootWebs! Very annoying. Great blog post in the link though. Thanks. – Mark Jun 14 '11 at 12:04
  • HA! I just used your article the other day for that same purpose! – brian brinley Jun 15 '11 at 14:31
4

No you should not. You should only dispose objects you are in control of. Because the context is something created by SharePoint you do not dispose of this as other objects may be dependent upon this.

If you were to create your own instance of an SPWeb from this objects properties than it would need to be disposed. I.e..

using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url))
using (SPWeb web = site.OpenWeb()) {
 // do something
}

Here is an article on the best practices of disposing SharePoint objects.

http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

brian brinley
  • 2,364
  • 1
  • 13
  • 10
  • Further comment specifically on SPContext.Current.Site.RootWeb - http://blogs.msdn.com/b/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx – Ryan Jun 13 '11 at 15:56
0

You should normally use SPSite and SPWeb in a using clause.

using (SPSite site = new SPSite("http://mysite.sharepoint.com"))
{
    using (SPWeb web = site.OpenWeb())
    {
        // TODO: code for using SPWeb object
    }
}

This automatically will correctly release the SPWeb object after you are done with it.

bdparrish
  • 3,216
  • 3
  • 37
  • 58
  • you should use a using clause only on disposable object you are in control of. SPContext is not something you are in control of and therefore should be disposed of. – brian brinley Jun 13 '11 at 12:32
  • @brian brinley: You're correct, but @bdparrish's example is correct too. Nothing obtained from `SPContext` is being disposed here. I don't think his answer should have been downvoted. – CBono Jun 13 '11 at 12:44
  • good point, but it doesn't answer the OP question. I have removed the DV though. – brian brinley Jun 13 '11 at 13:25
  • @brian brinley: the article you gave specifically points out my example as the way to handle the SPSite and SPWeb objects in code...[link](http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx#sharepointobjmodel__codingtechniques) – bdparrish Jun 15 '11 at 11:48
  • The key here is context. You're code isn't wrong, just shouldn't be used for the OP question. – brian brinley Jun 15 '11 at 14:29
  • So, because my code was the same as yours, but I didn't specifically state why he should not dispose of it, then I am wrong in context? I am missing two sentences from what your post states...:\ – bdparrish Jun 15 '11 at 18:21