0

I need to know how many components final layout has for each language programmatically. Is it any Sitecore API for it ?

2 Answers 2

12

This can be done by parsing the layout XML for each field (renderings and final renderings). The key is to use Sitecore.Data.Fields.LayoutField.GetFieldValue in order to obtain the XML value, otherwise you won't get the full XML with delta applied (final renderings --> shared renderings --> standard values).

In Sitecore PowerShell I was able to make it work with something like this:

$item = gi /sitecore/content/Home -Language 'en-US'

$sharedLayoutField = $item.Fields[[Sitecore.FieldIDs]::LayoutField]
$finalLayoutField = $item.Fields[[Sitecore.FieldIDs]::FinalLayoutField]

$sharedLayoutXml = [Sitecore.Data.Fields.LayoutField]::GetFieldValue($sharedLayoutField)
$finalLayoutXml = [Sitecore.Data.Fields.LayoutField]::GetFieldValue($finalLayoutField)

$sharedLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($sharedLayoutXml)
$finalLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($finalLayoutXml)

$sharedLayout.Devices[0].Renderings.Count
$finalLayout.Devices[0].Renderings.Count

In C#, something similar to this should do the trick.

// Returns the number of shared and final renderings for an item.
// Final renderings are based on language/version of item.
// This presumes there is only one device with renderings configured.
public static System.Tuple<int, int> CountRenderings(Sitecore.Data.Items.Item item)
{
    var sharedRenderingsCount = CountRenderings(item, Sitecore.FieldIDs.LayoutField);
    var finalRenderingCount = CountRenderings(item, Sitecore.FieldIDs.FinalLayoutField);
    return Tuple.Create(sharedRenderingsCount, finalRenderingCount);
}

private static int CountRenderings(Sitecore.Data.Items.Item item, Sitecore.Data.ID renderingFieldId)
{
    var field = item.Fields[renderingFieldId];
    var layoutXml = Sitecore.Data.Fields.LayoutField.GetFieldValue(field);
    var layout = Sitecore.Layouts.LayoutDefinition.Parse(layoutXml);
    var deviceLayout = layout.Devices[0] as Sitecore.Layouts.DeviceDefinition;
    return (deviceLayout?.Renderings.Count) ?? 0;
}

Of course, if you are using multiple devices, you will have to change the logic to pass in a device as parameter and return rendering count only for that device.

If you want to compare languages as well, you'll have to check the final renderings for each language variant.

Some reading:

2

You should be able to get a list all the renderings by calling:

item.Visualization.GetRenderings(Sitecore.Context.Device, true);
2
  • 1
    Is it get just final layout rendering ? what if I need to know how many components on shared just for compare amount ? Mar 16, 2017 at 16:34
  • how can I fetch Final Rendering using item.Visualization.GetRenderings(Sitecore.Context.Device, true); ?
    – Shaurya
    Nov 21, 2018 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.