Click here to view the version of this FAQ entry for Sitecore 5.2.
Can I get the DataSource set and Parameters for a sublayout via code-behind?
Codebehind
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Query;
using Sitecore.Configuration;
using Sitecore.Express;
using Sitecore.Layouts;
using Sitecore.SecurityModel;
using Sitecore.Sites;
...
protected void GetDataSource_Click(object sender, EventArgs e)
{
Database db = Sitecore.Context.Database;
Item item = db.Items["/sitecore/templates/SC Printers/Section"];
string rend = item.Fields["__renderings"].Value;
LayoutDefinition layout = LayoutDefinition.Parse(rend);
DeviceItem dev = Sitecore.Context.Device;
DeviceDefinition device = layout.GetDevice(dev.ID.ToString());
Item mySublayout = db.Items["/sitecore/layout/Sublayouts/Two columns"];
RenderingDefinition rendering = device.GetRendering(mySublayout.ID.ToString());
Response.Write("Parameters: " + rendering.Parameters);
Response.Write("<br/>");
Response.Write("DataSource: " + rendering.Datasource);
}