开发者

Sitecore: How to get sublayout ITEM from sublayout codebehind?

I'm trying to get the sublayout item开发者_开发技巧 (or item id) in the codebehind. Is this possible?

Update:

I do not mean the Datasource item or the Current item, I'm talking about the Sublayout rendering definition item. This has a 1-to-1 relationship with the sublayout file.

Codebehind file:

public partial class Product_Sublayout : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Sublayout subLayout = this.Parent as Sublayout;
        Item subLayoutItem = null; //How to get sublayout rendering definition item here?
    }
}


This page will explain the details, but here's the code that you need:

Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);

UPDATE:

You can probably get the rendering item itself by using the database to get it by ID:

Sitecore.Context.Database.GetItem(((Sublayout)Parent).RenderingID);


It is not required to loop through the controls or to check the sublayouts by the control name.

The correct way to go about the task is by following these steps:

  1. Get the LayoutDefinition for the the current item.
  2. Using the LayoutDefinition, get the RenderingDefinition of the sublayout you are interested in.
  3. Get the index of the sublayout against the complete list
  4. Use the index to retrieve the RenderingReference for the sublayout
  5. Use the RenderingReference to get the reference to the .NET control.

Below is the Code for achieving your goal:

LayoutDefinition layoutDef = LayoutDefinition.Parse(Sitecore.Context.Item.Fields["__renderings"].Value);
string deviceId = Sitecore.Context.Device.ID.ToString();
DeviceDefinition curDeviceDef = layoutDef.GetDevice(deviceId);
RenderingDefinition renderingDef = curDeviceDef.GetRendering(Sitecore.Context.Database.Items["/sitecore/Layout/SubLayouts/MySublayout"].ID.ToString());
int controlIndex = curDeviceDef.GetIndex(renderingDef.UniqueId);
Control MyDotNetControl = Sitecore.Context.Page.Renderings[controlIndex].GetControl();

Now you have the reference to your dot net control. Just type cast it to your corresponding control to get complete access of the object.


To get the Item that is used to place the particular Control to the page you can use a Shared Source Module called Sublayout Parameter Helper. The module can be found here

If you want to retrieve the Item you could consider using the following setup:

Props:

public partial class Afbeeldingen : System.Web.UI.UserControl
{
    /// <summary>
    /// Datasource item of the current rendering
    /// </summary>
    private Sitecore.Data.Items.Item dataSource = null;

    /// <summary>
    /// Helper object to get the rendering params, datasource and rendering
    /// </summary>
    private SublayoutParamHelper paramHelper = null;

    /// <summary>
    /// Gets the data source item.
    /// </summary>
    /// <value>The data source item.</value>
    public Sitecore.Data.Items.Item DataSourceItem
    {
        get
        {
            if (this.dataSource == null)
            {
                if (this.paramHelper.DataSourceItem != null)
                {
                    this.dataSource = this.paramHelper.DataSourceItem;
                }
                else
                {
                    this.dataSource = Sitecore.Context.Item;
                }
            }

            return this.dataSource;
        }
    }

    /// <summary>
    /// Sets the data source.
    /// </summary>
    /// <value>The data source.</value>
    public string DataSource
    {
        set
        {
            this.dataSource = Sitecore.Context.Database.Items[value];
        }
    }

Page_Load:

/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/></param>
protected void Page_Load(object sender, EventArgs e)
{
if (this.Parent is Sitecore.Web.UI.WebControls.Sublayout)
{
    this.paramHelper = new SublayoutParamHelper(this, true);
}

if (this.paramHelper != null)
{
    correctItem = paramHelper.DataSourceItem;
}

From there you have the Sub loaded in your correctItem. Hope this helps. Hope I understood your question well enough.


Here's a one-liner that gets you the Sublayout Item:

Sitecore.Context.Page.Renderings.Select(r => Sitecore.Context.Database.Items[r.RenderingID])
    .Where(item => item["Path"] == ((Sublayout)this.Parent).Path).Single();

Keep in mind that this will fail if you for some reason have multiple Sublayouts with the same Path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜