SaaS CMS has officially launched! Learn more now.

How to: Get block Items from a ContentArea based on its type

Ayo
Ayo
Vote:
 

I have a page type with a ContentArea as one of its properties

(currentPage.PrimaryComponentArea)

How can I get the block items stored in this property based on its type.

I also want to be to access properties on the block so I need to convert it from ContentAreaItem to the actuall block type.

 public ActionResult Index(RatePlanPageType currentPage)
        {
          ..........

          var allItems = currentPage.PrimaryComponentArea.Items;

          var blocks = allItems.Where(x => bla bla bla  < Can I do it using linq

        }

this is my first Episerver project so I hope this is not a stupid question.

#190200
Apr 04, 2018 18:28
Vote:
 

Check this https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2014/3/Iterating-through-ContentArea-items/

Items in ContentArea are saved as ContentReference, and all ContentReference reference to an IContent. A block and page type is an IContent.

But if you want just to display content on a page, you should use @Html.PropertyFor(x => x.CurrentPage.PrimaryComponentArea)

http://www.jondjones.com/learn-episerver-cms/episerver-developers-guide/episerver-mvc/how-to-display-episerver-blocks-in-your-mvc-views-using-the-propertyfor-helper

If you want to restrict contentareas to one type: [AllowedTypes(new [] {typeof(PageData), typeof(BlockData)})]

https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/Properties/Property-types/Restricting-content-types-in-properties/

Hope it helps

Regards

#190202
Edited, Apr 04, 2018 19:02
Vote:
 

You can get the content data from the items in the content area using Linq however, if you don't know the type of the items in the content area you'll have to get the content as a base type such as IContent:

var contentList = currentPage.PrimaryComponentArea?.FilteredItems?.Select(x => x.GetContent());

This uses the "GetContent" extension method in EPiServer.Core to return the IContent instance for the item. You may also notice that I'm using FilteredItems rather than Items as FilteredItems will remove items that the user shouldn't see due to permissions or personalisation.

#190205
Apr 04, 2018 20:17
Vote:
 

You can create helper functions:

public static class ContentAreaHelpers
{
    public static bool IsNullOrEmpty(this ContentArea contentArea)
    {
        return contentArea == null || !contentArea.FilteredItems.Any();
    }

    public static List<T> GetFilteredItemsOfType<T>(this ContentArea contentArea) where T : IContentData
    {
        var items = new List<T>();

        if (contentArea.IsNullOrEmpty())
        {
            return items;
        }

        var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

        foreach (var contentAreaItem in contentArea.FilteredItems)
        {
            IContentData item;
            if (!contentLoader.TryGet(contentAreaItem.ContentLink, out item))
            {
                continue;
            }
            if (item is T)
            {
                items.Add((T)item);
            }
        }

        return items;
    }
}

Usage:

var myItems = currentPage.PrimaryComponentArea
    .GetFilteredItemsOfType<MyType>()
    // ...
    .ToList();
#190211
Edited, Apr 04, 2018 22:19
Ayo
Vote:
 

Thank you Dejan Caric exactly what I was looking for.

#190224
Apr 05, 2018 10:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.