Sharepoint: How to obtain the current site/web/list properly

Posted by driAn on Stack Overflow See other posts from Stack Overflow or by driAn
Published on 2010-04-26T08:18:30Z Indexed on 2010/04/26 8:23 UTC
Read the original article Hit count: 247

Filed under:
|

Hi all

What is the best way to obtain the current site/web/list ?

Option 1 - Reusing existing objects

        SPSite site = SPContext.Current.Site;
        SPweb web = SPContext.Current.Web;
        SPList list = SPContext.Current.List;

Option 2 - Creating new objects

        SPSite site = new SPSite(SPContext.Current.Site.ID); // dispose me
        SPweb web = site.OpenWeb(SPContext.Current.Web.ID); // dispose me
        SPList list = web.Lists[SPContext.Current.List.ID];

I experienced problems when using option 1 in some situations. Since then I chose the 2nd option and it worked fine so far.

What is your opinion on this? I is generally better to go with option 2? Other suggestions?

© Stack Overflow or respective owner

Related posts about sharepoint

Related posts about sharepoint2007