Sunday, December 4, 2011

When to use SPContext.Current.Web and when to use SPControl.GetContextWeb(Context)

When your code file is a custom aspx, or code behind aspx.cs page hosted in a virtual directory
You can make use of :

SPWeb oWebSite = SPContext.Current.Web; or
SPWeb oWebSite = SPControl.GetContextWeb(Context);

to get reference to your current site.


If your .aspx page inherits from LayoutsPageBase instead of Page, you can use the Site or Web properties of SPContext.Current

If you want to use SPControl class to get reference to the current site or web object from  your .aspx page inherited from LayoutsPageBase

you can use the Microsoft.SharePoint.WebControls.LayoutsPageBase.Context property as the parameter to the GetContextWeb(HttpContext) and GetContextSite(HttpContext)

Eg:-
SPWeb oWebSite = SPControl.GetContextWeb(Microsoft.SharePoint.WebControls.LayoutsPageBase.Context);

No comments:

Post a Comment