0

If you open a SharePoint list like this

var site = new SPSite(url);
var web = site.OpenWeb();
var list = web.Lists["ListName"];

Is it OK to dispose site and web through properties of the list?

var web = list.ParentWeb;
var site = web.Site;
web.Dispose();
site.Dispose();

2 Answers 2

1

Yes. It's completely fine.

Either you can use like below code in using block:

using(SPSite oSPsite = new SPSite("http://server"))
{
  using(SPWeb oSPWeb = oSPSite.OpenWeb())
   {
       str = oSPWeb.Title;
       str = oSPWeb.Url;
   }
}  

You can get more information from here

Reference:

1

If you are explicitly creating object for each of them, then you are supposed to dispose that object also.

var site = new SPSite(url);
var web = site.OpenWeb();
var list = web.Lists["ListName"];

Here you will need to dispose each object explicitly as a good coding practice like this:

web.Dispose();
site.Dispose();
1
  • I don't think SPLists can be disposed Jun 20, 2016 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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