Return to doc.sitecore.com

Valid for Sitecore 5.2
Can I get the DataSource set and Parameters for a sublayout via code-behind?

Q: 

Can I get the DataSource set and Parameters for a sublayout via code-behind?

A: 

  1. Set a certain layout and sublayout for an item with Parameters and DataSource:
    /upload/sdn5/faq/api/sublayout via code-behind/sublayout_codebehind.jpg
  2. Publish.
  3. Use this code-behind code:

1.  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/content/home/Node1"];
        
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/MySublayout"];
         RenderingDefinition rendering
= device.GetRendering(mySublayout.ID.ToString());
         Response.Write(
"Parameters: " + rendering.Parameters);
         Response.Write(
"<br/>");
         Response.Write(
"DataSource: " + rendering.Datasource);
      }