AI OnAI Off
Hi,
GetContent is an extension method. Maybe you should add @using EPiServer.Core namespace to your view.
It's not related with your issue, but you could use FilteredItems property instead of Items property when iterating through ContentArea elements, because it will be filtered by content permissions and display options.
Here is an extension method:
/// <summary> /// Returns all the content items of <typeparamref name="T"/> from <paramref name="contentAreaItems"/> /// </summary> /// <typeparam name="T">Type of <see cref="IContent"/> items to extract.</typeparam> /// <param name="contentAreaItems">The <see cref="IEnumerable{ContentAreaItem}"/> to extract <see cref="IContent"/> instances of type <typeparamref name="T"/> from.</param> /// <param name="languageLoaderOption">The <see cref="LanguageLoaderOption"/> to use. If none is specified, <see cref="LanguageLoaderOption.FallbackWithMaster"/> is used.</param> /// <param name="contentLoader">The <see cref="IContentLoader"/> to use</param> /// <returns>A <see cref="IEnumerable{T}"/> containing all <see cref="IContent"/> items found in the <paramref name="contentAreaItems"/> collection.</returns> /// <remarks>The <paramref name="languageLoaderOption"/> does the same as what the <see cref="DefaultContentLoader"/> will do for a single content item if no language loader is supplied</remarks> public static IEnumerable<T> GetContentItems<T>(this IEnumerable<ContentAreaItem> contentAreaItems, LanguageLoaderOption languageLoaderOption = null, IContentLoader contentLoader = null) where T : IContent { if(contentAreaItems == null) { return Enumerable.Empty<T>(); } if(contentLoader == null) { contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); } if(languageLoaderOption == null) { languageLoaderOption = LanguageLoaderOption.FallbackWithMaster(); } return contentLoader.GetItems(contentAreaItems.Select(i => i.ContentLink), new LoaderOptions {languageLoaderOption}).OfType<T>(); }
And you could invoke it like so:
if(Model.CurrentPage.productResults != null) { Model.CurrentPage.productResults.FilteredItems.GetContentItems<ProductPage>(); }
Hi everyone!
I need someone who tells me how to get Content from every ContentAreaItem in a ContentArea which only allows items of type "ProductPage".
I'm trying to do the following but I get an error that says: 'EPiServer.Core.ContentAreaItem' does not contain a definition of 'GetContent' and no extension method 'GetContent' accepting a first argument of type 'EPiServer.Core.ContentAreaItem' (missing a usage policy or reference found or assembly reference?)