1

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))
    {
         // ..
    }
}

2 Answers 2

3

The 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.

1
  • Thank for response! I asked becouse was afraid if Web dispose() could implicitly call site dispose.
    – Gennady G
    Apr 23, 2015 at 15:34
2

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.

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.