Global Office Directory
More Products
Share this page
Home > Community > Technical Blogs > John West Sitecore Blog > How to Apply Data Sources to Sitecore ASP.NET Presentation Components
This blog post contains information about how presentation components can use data source items in the Sitecore ASP.NET CMS.
You can specify a data source for each XSL rendering, web control, or sublayout. Presentation components typically read data from their data source item. XSL renderings apply the data source automatically, but code in web controls and sublayouts must account for the data source. The default data source for each presentation component is the context item, which is the item requested by the browser. For more information about rendering parameters and data sources, see the Presentation Component Reference.
Specifying a data source for a presentation component does not set the context item to that data source, or otherwise cause the presentation component to access its data source item. Each presentation component must explicitly access its data source.
The default boilerplate code for XSL renderings automatically sets the context element to the data source of the rendering. I think this may be why so many developers assume that Sitecore will apply the data source as the context item in .NET presentation components. Technically, the code defined by the first <xsl:template> element in each XSL rendering sets the context element to the data source item, and then invokes the code contained in the second <xsl:template> element. Code within the second <xsl:template> that accesses the context element implicitly accesses the data source of the rendering. Note the difference between the context item, which is the item requested by the browser, and the context element, which is the location of the XSL transformation engine in the source XML document. For more information about XSL with Sitecore, see the Presentation Component XSL Reference.
In web controls, the DataSource property in the base class Sitecore.Web.UI.WebControl indicates the data source as a string, such as by path or ID. You can use the GetItem() method also defined in this base class to access the data source of the rendering.
Sublayouts are standard ASP.NET web user controls that do not inherit from a Sitecore class. As such, sublayouts do not expose a property to specify the data source. Sitecore uses the Sitecore.Web.UI.WebControls.Sublayout web control to invoke sublayouts. Sitecore.Web.UI.WebControls.Sublayout inherits from Sitecore.Web.UI.WebControl, which exposes the DataSource property. A sublayout can access its data source through this property in at least three ways:
string ds = ((Sitecore.Web.UI.WebControls.Sublayout)this.Parent).DataSource;
If you absolutely need to access the data source of a web control or sublayout through the Sitecore.Context.Item property, you might be able to use the Sitecore.Data.Items.ContextItemSwitcher class. For example, you might not have the source code for an existing web control that accesses Sitecore.Context.Item. For the scope defined by the using statement that creates an instance, the Sitecore.Data.Items.ContextItemSwitcher temporarily changes the context item. For example, to set the context item during the rendering phase of a sublayout, override the Render() method to use a ContextItemSwitcher to invoke the Render() method in the base class with the data source as the context item.
protected override void Render(HtmlTextWriter writer)
{
using(new Sitecore.Data.Items.ContextItemSwitcher(this.helper.DataSourceItem))
base.Render(writer);
}
Remember to use a Sitecore.Data.Items.ContextItemSwitcher in every method that needs to access the data source through Sitecore.Context.Item. For more information about .NET presentation components, see the Presentation Component API Cookbook.
To pass multiple data sources to a rendering, you can pass additional rendering parameters, or pass a data source containing a field that contains a multilist to specify those source items.
Tags: API, Architecture, Infrastructure
- Mark Ursino November 09, 2010 at 7:49 PM
- John West November 10, 2010 at 8:35 AM
- Mark Ursino November 10, 2010 at 10:58 AM
- Phoenix Zerin March 02, 2011 at 10:33 AM
- John West October 20, 2011 at 2:40 PM
- John West June 22, 2012 at 10:00 AM
- John West February 06, 2013 at 10:30 AM
John has over ten years of experience in the CMS industry. His areas of focus include the Sitecore community, Web industry research, Sitecore technical documentation, and product management.
This website is designed to be fully functional with scripts disabled in browser. Please contact the webmaster for any suggestions