Don't web.Dispose()
dispose parent SPSite
object?
Is it Ok if I do this in web part?
{
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb(s))
{
// ..
}
}
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityThe code you written is Ok.
using
statement will dispose the SPWeb
object you have instantiated.
Parent Site - This object is not initiantiated by you, you are using SPSite
that belongs to SPContext
.
SPContext
objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite
and SPWeb
objects returned by SPContext.Site
, SPContext.Current.Site
, SPContext.Web
, and SPContext.Current.Web
.
Yes, it's acceptable to do what you have done. It's also ok to pass that web object to other functions.
See a detailed article on this subject from Microsoft here.