Copied to clipboard

Flag this post as spam?

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


  • ds 191 posts 223 karma points
    May 22, 2015 @ 22:51
    ds
    0

    Loop through all images in a folder?

    Hi All,

    I have a folder on media section called Slider. Under that folder there are many images. I also added my document type a media picker called pictures. After pointing slider folder,

    How can I loop through all the images by getting the url under Slider folder on media section using razor?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 22, 2015 @ 23:45
    Dennis Aaen
    0

    Hi ds,

    You can loop through all the images by using this piece of code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{      
    
        if (CurrentPage.HasValue("pictures")){                                         
    
            var dynamicMediaFolder = Umbraco.Media(CurrentPage.pictures);
    
             <ul>
    
                @foreach (var picture in dynamicMediaFolder.Children)
    
                {
    
                    <li>
    
                        <a href="@picture.Url">
    
                            <img src="@picture.umbracoFile" alt="@picture.Name" />
    
                        </a>
    
                    </li>
    
                }
    
            </ul>
    
        }
    
    }
    

    Then you will print the image in a unordered list and get the url to the images. In my example I am using partial view macro files, but you can also use partial views

    You are saying that you are using the content picker, then I take the seleted id of the folder and loop through the children (the images). You can find the documentation for the content picker here: https://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Media-Picker

    I have used the dycnamic Razor, but you can also use the strongly typed Razor.

    Hope this helps,

    /Dennis

  • ds 191 posts 223 karma points
    May 23, 2015 @ 13:26
    ds
    0

    Hi Dennis,

    It works like a charm. Thank you very much for the help. I only changed first line

    inherits Umbraco.Web.Mvc.UmbracoTemplatePage

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 23, 2015 @ 13:40
    Dennis Aaen
    0

    Hi ds,

    Sounds nice that it works like intended.

    Yes then you are using Partial Views instead of Partial Views Macros.

    /Dennnis

Please Sign in or register to post replies

Write your reply to:

Draft