Search code examples
c#asp.netepiserver

EPiServer 9: Is there any real alternative for EPiServer ContentArea Contents?


I am a beginner regarding EPiServer. We use EPiServer Version 9.12. EPiServer.Core.ContentArea had a Contents list in the past which is now obsolete, see: http://world.episerver.com/documentation/Class-library/?documentId=cms/7.5/284B326A

image http://jweschenfelder.de/download/Untitled.png

The Contents list had the advantage in the past that you could read the name of the block because it read the full content of the ContentArea. It would be great to retrieve the name since you can configure it inside the CMS if you create a new block there. If I use the now suggested Items collection instead, I am not able to read the name of the block which contains the Link items collection, I am only able to read the Link items collection inside of the block then.

I have seen the example:
IContentLoader contentLoader = ServiceLocator.Current.GetInstance< IContentLoader >(); OnSiteLinkBlock itemBlock = contentLoader.Get(item.ContentLink, new LoaderOptions() { LanguageLoaderOption.MasterLanguage() });
I am able to edit OnSiteLinkBlock, but additional properties remain null and are not filled by the ContentLoader of the EPiServer (IContentLoader is an interface of EPiServer).

More info about the class hierarchy:
- [AvailableContentTypes(Availability = Availability.None)]
public class BlockData : ContentData, IReadOnly< BlockData >, IReadOnly
(in EPiServer.Core)
- public abstract class BlockBase : BlockData (BlockBase is an own class)
- public class OnSiteLinkBlock : BlockBase (OnSiteLinkBlock is an own class)

Does someone know a solution here? How can I read more properties of the ContentArea? Or does exist an alternative for ContentArea? Many thanks!


Solution

  • Typically you use the Items or FilteredItems properties to read contents from ContentAreas. They return an enumerable of ContentAreaItem's.

    Resolve the IContent instance using an IContentLoader and feed it with the ContentLink

    var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
    
    // contentarea is called UpperArea in the example
    var icontentItems = currentPage.UpperArea
                             .FilteredItems
                             .Select(x => loader.Get<IContent>(x.ContentLink));
    
    // example render in razor
    foreach (var icontentItem in icontentItems)
    {
        <h2>@icontentItem.Name</h2>
    }