get content from ContentArea's first item's property

Vote:
 

I have a listing page that pulls info from its child pages.  Each child page has a property called 'Images' that is a ContentArea of blocks - with each block containing an 'Image' property. On the listing page, for each child page listed, I want to get the first block's Image property's src path. How would I do that? This is how I'm pulling in each child page...

@foreach (ReoListingPage listing in Model.Locations.Skip(Model.StartRowIndex).Take(Model.Count))
        {}

I hope that makes sense what I'm asking. Thank you!

#147072
Apr 05, 2016 17:23
Vote:
 

1. Get the page

2. Get the content area property (images) 

3. Use the content areas Items collection. Grab the first content item of the type you are looking for.

4. Use the Image property to render src. If this property is of type content or media you can use UrlResolver.GetUrl to get path.

Hope that helps 

#147073
Apr 05, 2016 17:46
Vote:
 

Thanks for the response Daniel. Unfortunately I am not an Epi expert. Could you expand on your response with code examples? The property is a media type. So within my foreach loop above, I would think I'd start with 

@UrlResolver.Current.GetUrl(listing.Images.Items)

but after that, I'm don't know.

#147075
Apr 05, 2016 18:12
Vote:
 
 var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
            if (currentPage.MainContentArea.Items.Any())
            {
                foreach (var contentAreaItem in currentPage.MainContentArea.FilteredItems)
                {
                    var contentItem = contentLoader.Get<IContent>(contentAreaItem.ContentLink) as MediaData;
                    if (contentItem != null)
                    {
                        var url = UrlResolver.Current.GetUrl(contentItem);
...do something with the url. Fill custom viewmodel etc...
                    }
                }
            }

Something like that...took alloy start page as example. If you use FilteredItems you get support for visitor groups etc. I would probably put the above code in the controller and add it to the viewmodel in some custom field. A bit too much code to have in view in my point of view but thats a matter of taste...

#147077
Edited, Apr 05, 2016 18:36
* 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.