Search code examples
c#sharepoint-2010webspcontext

What the difference between SPContext.Current.Web.Site.OpenWeb().Lists["List"]; and SPContext.Current.Web.Lists["List"]?


I have to refactor some sharepoint 2010 code from my collegue. Everytime he needs to access a list he does this:

SPContext.Current.Web.Site.OpenWeb().Lists["List"];

I used to do this:

SPContext.Current.Web.Lists["List"];

What's the difference between these two and whats the more efficient way?


Solution

  • The second is much more efficient way.

    In the first method, you are creating a new SPWeb object through the OpenWeb() call which is an expensive call. Note only that, you must also explicitly dispose this object manually when you are done using that.

    Read here: http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx