Some Basics of sharepoint which everyone should be aware of
Never create a sharepoint object with SPContext.Current.Site
using
(
SPSite
site =
SPContext
.Current.Site)
{
}
Well whats wrong with the above declaration?
If we see it closely we are creating a site obect in Using block which will dispose the sharepoint object for a current request hence it would cause errors in the next instances of spsite object
Always create a sharepoint object with below
using
(
SPSite
site =
new
SPSite
(
SPContext
.Current.Site.Url))
{
}
Root Site Url
The below line of code will always return the url of root site
string
rootSiteUrl =
SPContext
.Current.Site.Url;
Current web Url
The below line of code will always return the url of current web
string
currentWebUrl =
SPContext
.Current.Web.Url;
Objects used in Feature Receiver
The object returned by the SPFeatureReceiverProperties.Feature.Parent property for a site- or web-scoped feature receiver should not be disposed of:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
// Do stuff
}
Objects owned by Sharepoint
Some objects are owned by SharePoint and must not be disposed of in custom code. The SPWeb object returned by SPSite.RootWeb is such an example.
This property returns a shared SPWeb instance and is used internally in several methods and properties. It should never be disposed of explicitly. The following sample code does not dispose of anything:
SPSite site = SPContext.Current.Site;
SPWeb rootWeb = site.RootWeb;
SPWeb web = SPContext.Web;
SPWeb web = SPContext.Current.Web;
SPSite site = SPContext.Site;
SPSite site = SPContext.Current.Site;
No comments:
Post a Comment