Defining a Sitecore Access Schema Programmatically

I want to access the layout description of an element so that I can access the visualization added to the element and then access the data sources attached to the specified visualization. I can't seem to find a way to do this. The best I could do was access the field __renderings, but then it turned out that it would be access to the original element of the rendering definition, and not to a specific instance with the data stored in the design layout.

This is on Sitecore 7.5 MVC

If this helps, this is what I tried to do:

// Get the default device
DeviceRecords devices = item.Database.Resources.Devices;
DeviceItem defaultDevice = devices.GetAll().Where(d => d.Name.ToLower() == "default").First();

// Get the rendering references for the default device
Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__renderings"];
Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(defaultDevice);

// Get the required renderings
RenderingItem headerRenderingItem = null;
RenderingItem aboutRenderingItem = null;

foreach (var rendering in renderings)
{
    if (rendering.Placeholder == "headerPlaceholder")
        headerRenderingItem = rendering.RenderingItem;
    else if (rendering.Placeholder == "/aboutSectionPlaceholder/textPlaceholder")
        aboutRenderingItem = rendering.RenderingItem;
}
Assert.IsNotNull(headerRenderingItem, "no header rendering item found");
        Assert.IsNotNull(aboutRenderingItem, "no about rendering item found");

// Get their datasources
ID headerDatasourceId = ID.Parse(headerRenderingItem.DataSource); // The datasource string is null as it is accessing the datasource definition item itself
ID aboutDatasourceId  = ID.Parse(aboutRenderingItem.DataSource);  // Same as above
+4
source share
1 answer

RenderingReference.RenderingItem /​​layout. RenderingReference.Settings.Datasource.

, :

foreach (var rendering in renderings)
{
    if (rendering.Placeholder == "headerPlaceholder")
        headerRenderingDatasourceId = rendering.Settings.Datasource;
    else if (rendering.Placeholder == "/aboutSectionPlaceholder/textPlaceholder")
        aboutRenderingDatasourceId = rendering.Settings.Datasource;
}

Item headerRenderingDatasourceItem;
if (!string.IsNullOrEmpty(headerRenderingDatasourceId)
    headerRenderingDatasourceItem = item.Database.GetItem(headerRenderingDatasourceId);
+4

Source: https://habr.com/ru/post/1622295/


All Articles