Sharepoint: How to obtain the current site/web/list properly
- by driAn
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?