Sitecore How to get the benchmark of a data source
Is it possible to get the value of the __Rendering control template field for a content item? Specifically, I would like to get the value of the Data Source field defined in a control on the page as shown below.
As shown in the screenshot, I have controls in a page element and I would like to get the value of the Data Source field.
I used this code and I could list all the controls using the page element. But I don't know how to get information about the data sources viewed in the control page.
public RenderingReference[] GetListOfSublayouts(string itemId, Item targetItem)
{
RenderingReference[] renderings = null;
if (Sitecore.Data.ID.IsID(itemId))
{
renderings = targetItem.Visualization.GetRenderings(Sitecore.Context.Device, true);
}
return renderings;
}
public List<RenderingItem> GetListOfDataSource(RenderingReference[] renderings)
{
List<RenderingItem> ListOfDataSource = new List<RenderingItem>();
foreach (RenderingReference rendering in renderings)
{
if (!String.IsNullOrEmpty(rendering.Settings.DataSource))
{
ListOfDataSource.Add(rendering.RenderingItem);
}
}
return ListOfDataSource;
}
RenderingReference[] renderings = GetListOfSublayouts(targetItem.ID.ToString(), targetItem);
List<RenderingItem> ListOfDataSource = GetListOfDataSource(renderings);
source to share
This is exactly what I wanted.
Works great !!!!!!
public IEnumerable<string> GetDatasourceValue(Item targetItem)
{
List<string> uniqueDatasourceValues = new List<string>();
Sitecore.Layouts.RenderingReference[] renderings = GetListOfSublayouts(targetItem.ID.ToString(), targetItem);
foreach (var rendering in renderings)
{
if (!uniqueDatasourceValues.Contains(rendering.Settings.DataSource))
uniqueDatasourceValues.Add(rendering.Settings.DataSource);
}
return uniqueDatasourceValues;
}
}
source to share
Here is a blog post that might help: Using a Data Source Field with Sitecore Subcategories
Here's the relevant code you can call from a single control:
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;
}
}
When you add the property DataSource
defined above, you will get the item designated as data source from the CMS.
source to share