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.
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.
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.
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'
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?
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.
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.
Thank you, Ambert.
But this code won´t loop recursive (deeply nested folders), will it?
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:
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 ;-)
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?
Disregard that, seems to be working perfectly now. Not sure what was going on before.
Thanks! :)
is working on a reply...