Should I get rid of this SPWeb?

I hope someone can help me. I need to get the root network of the current site from SPContext. This is easy to do using the following

SPContext.Current.Site.RootWeb 

I like the idea that the SPSite object is here in SPContext.Current. The .RootWeb site should not be deleted, but what about the SPWeb object that I get from SPSite. Will there be a rootWeb SPWeb when SPSite is deleted? Or do I need to get rid of it myself?

+6
source share
3 answers

Calls to SPSite.RootWeb should not be deleted. Recycling SPSite will also use RootWeb.

There is an error in SPDisposeCheck in which it is flagged if you destroy it, and if not (damned anyway!), I described in detail how I solved this in this blog post , since you cannot use the SPDisposeCheckIgnore attribute in blocks with elevated privileges.

+6
source

No, not worth it. You should only dispose of the objects that you manage. Since the context is something created by SharePoint, you do not dispose of it, as other objects may depend on it.

If you were to create your own instance of SPWeb from these object properties, then it should have been deleted. Ie.

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

Here's an article about best practices for deleting SharePoint objects.

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

+4
source

Usually you should use SPSite and SPWeb in the usage section.

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

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

0
source

Source: https://habr.com/ru/post/890397/


All Articles