Sharepoint: how to get the current site / web list correctly

What is the best way to get the current site / web listing?

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 am having trouble using option 1 in some situations. Since then, I have chosen the second option, and so far it has worked fine.

How do you feel about this? Am I better off going with option 2? Other offers?

+3
source share
2 answers

, , (, OpenWeb ). SPContext, .

Option Two, (, SharePoint timer Workflow), SPContext.Current null.

, SPWeb SPSite-, .

+4

2 (, , Microsoft), , using, , . :

using (SPSite site = new SPSite("MY SITE URL"))
{
  using (SPWeb web= site.OpenWeb())
   {
       // Do stuff
   }
} 

, .

+2

Source: https://habr.com/ru/post/1742770/


All Articles