Disposing SharePoint Objects
Question
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();
Solution
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:
OTHER TIPS
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange