What is difference between
SPSite spSite = SPContext.Current.Site;
and
SPSite spsite = new SPSite(SPContext.Current.Web.Url)
Which is good practice?
SPSite spsite = new SPSite(SPContext.Current.Web.Url)
creates a new instance of the SPSite
object, and you are responsible to dispose it. However, you don't need to dispose SPSite
in SPSite spSite = SPContext.Current.Site;
Well the first on uses the already existing site in the SPContext object.
The second one initiate a new SPSite object (from the url of the current site).
The second one should primarily be used when you use SPSecurity.RunWithElevatedPrivileges
. That is because otherwise the site object will not be elevated.
Also, the second one is used when working with another site then the current (by passing in GUID or Url)