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)
If you want to restrict contentareas to one type: [AllowedTypes(new [] {typeof(PageData), typeof(BlockData)})]
Hope it helps
Regards
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.
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();
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.