user3224222
user3224222

Reputation: 412

Sitecore. Read items from placeholder

I have the page with placeholder and list. I want to be able to fill the list with information depending on the items in the placeholder. For example:

I have "Person" template with Name and Image Fields. I creat few items based on that template (person A, B and C). After adding the renderings with these items as a source, I display images in the placeholder. Now I want to get all the item that are currently in the placeholder and write theirs Name field into the list(which is outside placeholder).

For now I was just able to get the rendering item, but not source item, out of placeholder.

Upvotes: 3

Views: 1576

Answers (2)

user3224222
user3224222

Reputation: 412

I was able to get source items by Id that I found in Settings of RenderingItem:

var ph = "my_placeholder";
var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith('/' + ph, StringComparison.OrdinalIgnoreCase));
var items = renderingsInPlaceholder.Select(x => context.GetItem(ID.Parse(x.Settings.DataSource)));

Upvotes: 1

Vlad Iobagiu
Vlad Iobagiu

Reputation: 4118

You can try bellow method:

   /// <summary>
   /// Return all renderings to be rendered in a specific placeholder on the "default" device
   /// </summary>
   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;
         }
       }
   }

Upvotes: 1

Related Questions