Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 162 posts 586 karma points c-trib
    Mar 16, 2022 @ 09:46
    Martin Rud
    0

    Loop all (image) files in multiple and nested media folders

    I have a property, named 'folders', that is of type Multiple Node Tree Picker, and node type is set to 'Media" and Allow items of type is set to 'Folder'.

    Therefore editors can choose multiple folders (also a folder that is inside another folder that is also picked by the editor).

    Now I want to loop all image files in these folders. I guess I have to make some recursive loop that adds the images it finds to a list. But I am neither an expert in recursive looping code nor adding media items to a list.

    Have anyone a sample code? enter image description here

  • Ambert van Unen 158 posts 766 karma points c-trib
    Mar 16, 2022 @ 10:35
    Ambert van Unen
    0

    It's not hard to do though, just treat it like any other list of items you can loop through.

    So basically you get something like this (copy pasted from a own project), here it selects ONLY the child images from each selected folder. You can probably modify this to suit your needs.

     var imagesFolders = Model.Images; //Your MNTP
     foreach(var imageFolder in imageFolders){
            if (imagesFolder.Children().OfType<Image>().Any())
            {
                var images = imagesFolder.Children().OfType<Image>().Any();
                foreach (var image in images)
                {
                    var imageUrl = image.GetCropUrl(1024, 768, "umbracoFile", null, 70);
                    }
                    <span><img src="@imageUrl" alt="@item.Name" /></span>
                }
    
        }}
    

    This will result in a big list of images, if you want it sorted by folder/section, you can just wrap some HTML around the foreach, or when used in a Controller, add the items to a list.

  • Martin Rud 162 posts 586 karma points c-trib
    Mar 16, 2022 @ 11:02
    Martin Rud
    0

    Thank you, Ambert.

    But this code won´t loop recursive (deeply nested folders), will it?

  • Ambert van Unen 158 posts 766 karma points c-trib
    Mar 16, 2022 @ 11:23
    Ambert van Unen
    100

    Depends on what you want, just the images for the selected folders, or descendant images aswell.

    If the latter, you could do something like this, this can be quite resourceful though because you iterate over anything:

                var imagesFolders = imagePicker; //Your MNTP
                var allImages = new List<IPublishedContent>();
                foreach (var imageFolder in imageFolders)
                {
                    if (imageFolder.Descendants().OfType<Image>().Any())
                    {
                        var images = imageFolder.Descendants().OfType<Image>();
                        foreach (var image in images)
                        {
                            allImages.Add(image);
                        }
                    }
                }
    

    After this allImages contains all images, but also duplicates if you have selected a folder and also it's subfolder. If you don't want that you need to filter this list, or check if it's already added before adding it to the list, not sure what the best performer is here.

    Good luck ;-)

  • Amir Khan 1272 posts 2685 karma points
    Apr 28, 2022 @ 21:21
    Amir Khan
    0

    Using literally this exact setup and code I'm getting the error CS1579: foreach statement cannot operate on variables of type 'MediaWithCrops' because 'MediaWithCrops' does not contain a public instance definition for 'GetEnumerator'

    for this foreach loop, what am I missing here?

    foreach (var imageFolder in imageFolders)
    
  • Amir Khan 1272 posts 2685 karma points
    Apr 28, 2022 @ 21:36
    Amir Khan
    0

    Disregard that, seems to be working perfectly now. Not sure what was going on before.

  • Martin Rud 162 posts 586 karma points c-trib
    Mar 16, 2022 @ 12:47
    Martin Rud
    0

    Thanks! :)

Please Sign in or register to post replies

Write your reply to:

Draft