Copied to clipboard

Flag this post as spam?

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


  • Mark Evans 86 posts 116 karma points
    Sep 01, 2014 @ 13:09
    Mark Evans
    0

    getting images from a media folder

    what is the best approach to getting images from a particular folder (within media) in umbraco?

    i just want to get a set of images and loop thru them but cannot seem to get the right syntax to access the images....

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2014 @ 13:13
    Jeavon Leopold
    0

    Hi Mark,

    Something like this:

    @{
        var myMediaFolder = Umbraco.Media(1234);
        foreach (var mediaItem in myMediaFolder.Children)      
        {
            <li><a href="@mediaItem.Url">@mediaItem.Name</a></li>
        }  
    }
    

    If you have media picker to select the folder then change 2nd line to:

    var myMediaFolder = Umbraco.Media(CurrentPage.myMediaPickerAlias);
    

    I hope that makes sense?

    Jeavon

  • Mark Evans 86 posts 116 karma points
    Sep 01, 2014 @ 13:39
    Mark Evans
    0

    yes thats great, thanks...

    just a quick question, can i use the getCropUrl (imagecropper) extension with this format as i can when accessing a property value ?

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2014 @ 13:43
    Jeavon Leopold
    0

    Sure, if your Cropper has been put in place of the default Upload on the Image media type, then:

    @{
        var myMediaFolder = Umbraco.Media(1234);
        foreach (var mediaItem in myMediaFolder.Children)
        {
            <img href="@mediaItem.GetCropUrl("cropAlias")" />
        }
    }
    

    You might be interested in watch this video about using the Image Cropper

    Jeavon

  • David Armitage 508 posts 2078 karma points
    Oct 03, 2019 @ 05:48
    David Armitage
    0

    Hi Guys,

    Here is a good method I use to firstly to get the containing media folder by name. You can then look through the children.

    public static IPublishedContent GetFolderByName(string name, int? parentId = null)
            {
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
                if(parentId != null)
                {
                    umbracoHelper.TypedMedia(parentId).Children("Folder").Where(x => x.Name == name).FirstOrDefault();
                }
    
                return umbracoHelper.TypedMediaAtRoot().DescendantsOrSelf("Folder").Where(x => x.Name == name).FirstOrDefault();
            }
    

    Then call the method here

    var mediaFolder = MediaManager.GetFolderByName("Some Folder Name");
                foreach (var mediaItem in mediaFolder.Children)
                {
                    //do whatever you want with the media here
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft