November Happy Hour will be moved to Thursday December 5th.

How to get Content from ContentAreaItem

Vote:
 

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".

[AllowedTypes(typeof(ProductPage))]
        public virtual ContentArea productResults { get; set; }

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?)

@if (@Model.CurrentPage.productResults!=null){
        @foreach (var item in @Model.CurrentPage.productResults.Items)
        {
            var product = (ProductPage)item.GetContent();
...
}
#133177
Aug 27, 2015 13:13
Vote:
 

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.

#133180
Aug 27, 2015 13:52
Vote:
 

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>();
}



#133201
Aug 27, 2015 14:43
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.