1

I am trying to create a report of all placeholders and the rendering options available inside them. For this, I want to get all renderings within a placeholder.

Is there a way get I can get all the renderings available in a placeholder in Sitecore using the Sitecore API?

Thanks

1

You can use :

private IEnumerable<Sitecore.Data.Items.RenderingItem> GetRenderings(string placeholderKey, Sitecore.Data.Items.Item item)
{
  Sitecore.Layouts.RenderingReference[] renderings = GetRenderingReferences(item, "default");
  foreach (var rendering in renderings)
  {
    if (rendering.Placeholder == placeholderKey)
    {
      yield return rendering.RenderingItem;
    }
  }
}


private Sitecore.Layouts.RenderingReference[] GetRenderingReferences(Sitecore.Data.Items.Item item, string deviceName)
{
  Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__renderings"];
  Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(GetDeviceItem(item.Database, deviceName));
  return renderings;
}

Here you can find multiple methods about placeholders API: https://briancaos.wordpress.com/2013/12/17/get-sitecore-placeholders-and-rendering-hierarchy-from-a-sitecore-item/

  • Is there a way I can use this code without specifying an item and only a placeholder key? – Gabbar Aug 15 '18 at 17:53
  • nope, because "__renderings" field is a field on an item. – Vlad Iobagiu Aug 15 '18 at 17:54
  • Alright. I was trying to avoid using an item to get the renderings but I haven't seen any other way so I will use some existing items as examples. – Gabbar Aug 15 '18 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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