Sitecore 8: Datasource value is null/empty in sublayout(.ascx)

772 Views Asked by At

I am trying to access datasource value of sublayout through code behind and I get empty string in a data source.

Below are the screen shots of my Banner sublayout in sitecore and how the datasource value is set in presentation:

Banner Sublayout with Datasource location and template set

Banner sublayout added to presentation and datasource set to an item

Below is the code that I am using to retrieve datasource:

    using Sitecore.Data.Items;
    using Sitecore.Web.UI.WebControls;
    using System;
    using System.Web.UI;

    public partial class Banner : UserControl
    {
        private Item _dataSource = null;
        public Item DataSource
        {
            get
            {
                if (_dataSource == null)
                    if (Parent is Sublayout)
                    {
                        _dataSource = Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
                    }
                return _dataSource;
            }
        }
        private void Page_Load(object sender, EventArgs e)
        {
            var test = DataSource;
            Item a = Sitecore.Context.Item;
        }
    }

Am I doing anything wrong or what could be the issue?

1

There are 1 best solutions below

0
On

What if you get your item renderings:

RenderingReference[] renderings = currentItem.GetRenderings();

Then, get the rendering you want using:

var rendering = renderings.Where(r => r.RenderingItem.ID != yourRenderingID)

And Finally,

rendering.Settings.DataSource  //This is the datasource id