Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ole Kirkholt 8 posts 38 karma points
    May 21, 2019 @ 05:04
    Ole Kirkholt
    0

    Get All Images from Media Folder Problem

    In Umbraco 7 I create an image gallery (bootstrap carousel) from all images in a selected folder (mappe). Below is my code for V7, but it doesn't work i V 8:

    @using System.Globalization
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner">
                            @{
                                bool first = true;  
                                var myMediaFolder = Umbraco.Media(@CurrentPage.Mappe);
                                foreach (var mediaItem in myMediaFolder.Children)      
                                {
                                    string aktiv = (first ? "active" : "");
                                    <div class="carousel-item @aktiv">
                                        <img src="@mediaItem.Url" alt="@mediaItem.Name" style="width: 100%">
                                    </div>
                                    first = false;
                                }  
                            }
                        </div>
    
  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    May 21, 2019 @ 06:43
    Søren Kottal
    0

    Hi Ole

    UmbracoTemplatePage and CurrentPage have been removed from Umbraco 8.

    In stead you should use UmbracoViewPage and strongly typed access to your properties. If you use modelsbuilder, that would be Model.Mappe.

    @inherits UmbracoViewPage
    
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner">
                            @{
                                bool first = true;  
                                var myMediaFolder = Model.Mappe; // or Model.GetValue<IPublishedContent>("mappe")
                                foreach (var mediaItem in myMediaFolder.Children)      
                                {
                                    string aktiv = (first ? "active" : "");
                                    <div class="carousel-item @aktiv">
                                        <img src="@mediaItem.Url" alt="@mediaItem.Name" style="width: 100%">
                                    </div>
                                    first = false;
                                }  
                            }
                        </div>
    
  • Ole Kirkholt 8 posts 38 karma points
    May 21, 2019 @ 19:03
    Ole Kirkholt
    0

    OK, now I have the folder (I can get it's name), but getting the children fails. How do I cast it as a media-folder

    Compiler Error Message: CS0446: Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?
    
    Source Error:
    
    
    Line 12:                            bool first = true;  
    Line 13:                            var myMediaFolder = Model.GalleriMappe;
    Line 14:                            foreach (var mediaItem in myMediaFolder.Children)      
    
  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    May 21, 2019 @ 19:07
    Søren Kottal
    0

    Try this

    foreach (var mediaItem in myMediaFolder.Children())     
    
  • Ole Kirkholt 8 posts 38 karma points
    May 21, 2019 @ 19:09
    Ole Kirkholt
    0
    Compiler Error Message: CS1929: 'IEnumerable<IPublishedContent>' does not contain a definition for 'Children' and the best extension method overload 'PublishedContentExtensions.Children(IPublishedContent, string)' requires a receiver of type 'IPublishedContent'
    

    Source Error:

    Line 12: bool first = true;
    Line 13: var myMediaFolder = Model.GalleriMappe; Line 14: foreach (var mediaItem in myMediaFolder.Children())

  • Magnus Eriksson 122 posts 362 karma points
    May 21, 2019 @ 19:22
    Magnus Eriksson
    0

    If Model.GalleriMappe is of IEnumerable<IPublishedContent> you should probably not call children since that model already contains a collection. Try:

    foreach (var mediaItem in myMediaFolder)  
    

    Regards, Magnus

  • Ole Kirkholt 8 posts 38 karma points
    May 21, 2019 @ 19:31
    Ole Kirkholt
    0

    That doesn't throw any errors, but gives me one elemen with the name of the chosen folder = Forside:

     <div class="carousel-item active">
                                        <img src="" alt="Forside" style="width: 100%">
                                    </div>
    
  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    May 21, 2019 @ 19:34
    Søren Kottal
    101
    foreach (var mediaItem in myMediaFolder.SelectMany(x => x.Children()))
    
  • Ole Kirkholt 8 posts 38 karma points
    May 21, 2019 @ 19:38
    Ole Kirkholt
    0

    Thanks a lot guys. That was the trick. Have a very nice evening :)

    <div class="carousel-inner">
                            @{
                                bool first = true;  
                                var myMediaFolder = Model.GalleriMappe;
                                foreach (var mediaItem in myMediaFolder.SelectMany(x => x.Children()))      
                                {
                                    string aktiv = (first ? "active" : "");
                                    <div class="carousel-item @aktiv">
                                        <img src="@mediaItem.Url" alt="@mediaItem.Name" style="width: 100%">
                                    </div>
                                    first = false;
    
                                }  
                            }
                        </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft