What I'm trying to achieve is to build a usercontrol that allows me to enter a filename (or partial) filename of a 'document' (could be pdf, png, jpg, doc, docx, pot, potx, etc etc) and upon submission I'd then like to search through the entire media section to see if anything in there matches the search.
I can search through a particular media folder by it's node but what I'd like to do is search every folder under in media section. And I can't seem to find anything to aide me in this.
But I can't for the life of me work out how to loop through ALL of the folders. Or even get a list of all of the media folders id's I really think that I must be missing something really simple!!
I know this post is old. I had to do this for a client, as the site was an ecommerce site w/ a large amount of products and sub products, etc. They didn't want to go through every one (product, etc) and assign the image(s), swatches, etc b/c that'd be time consuming. Therefore, I looped though the media folder for the file name. This does take some time depending on how many images you are looking for.
This is only an example:
public class FolderHelper
{
private string mediaPath = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Media");
private string swatchPath = "/images/swatches/";
/// <summary>
/// Gets swatch image from Umbraco Media folder or swatch folder
/// </summary>
/// <param name="color">color of the swatch</param>
/// <returns>swatch image path</returns>
public string GetSwatch(string color)
{
color = this.StripNonAlphaChars(color);
string swatch = Directory.GetFiles(mediaPath, string.Format("swatch_{0}.jpg", color), SearchOption.AllDirectories).FirstOrDefault();
return !string.IsNullOrEmpty(swatch)
? swatch.Substring(swatch.IndexOf(@"\Media"), swatch.Length - swatch.IndexOf(@"\Media")).Replace(@"\", "/")
: string.Format("{0}swatch_{1}.jpg", swatchPath, color);
}
}
So I found a way easier way of doing this, more to what you were looking for. Instead of looking in ALL directories under "media", this grabs the media folder by "Name" and then looks in the child list for the file by name. The image path is the first "property" index.
Loop through all media folders
Hi folks,
I wonder if someone could assist me please.....
What I'm trying to achieve is to build a usercontrol that allows me to enter a filename (or partial) filename of a 'document' (could be pdf, png, jpg, doc, docx, pot, potx, etc etc) and upon submission I'd then like to search through the entire media section to see if anything in there matches the search.
I can search through a particular media folder by it's node but what I'd like to do is search every folder under in media section. And I can't seem to find anything to aide me in this.
Can anyone help? Please!
Thanks,
Craig
Hi Graig,
Maybe this post could be a help at some point
http://our.umbraco.org/forum/developers/extending-umbraco/2295-iterate-media-nodes-with-api-%5Bsolved%5D
/Dennis
Thanks Dennis,
I shall give this a try and let you know how I go.
Thanks again :)
Hi, what version of umbrco. I believe i have done this by creating a dynamicnode (int your media folder start id)
you can just use of
foreach(Node node in DynamicNode) // use Linq
{
// do your stuff
}
Hiya,
Still stuck I'm afraid :(
I can loop through an individual folder in the media section using something the below:
Media imageFolder = new Media(1234);
foreach (Media i in imageFolder.GetChildMedia())
{
zURL = i.getProperty("umbracoFile").Value.ToString().ToLower().Trim();
if (zURL.Contains(myFileName))
{
//We have found the video sample
zVideoPath = zURL;
}
}
But I can't for the life of me work out how to loop through ALL of the folders. Or even get a list of all of the media folders id's I really think that I must be missing something really simple!!
I know this post is old. I had to do this for a client, as the site was an ecommerce site w/ a large amount of products and sub products, etc. They didn't want to go through every one (product, etc) and assign the image(s), swatches, etc b/c that'd be time consuming. Therefore, I looped though the media folder for the file name. This does take some time depending on how many images you are looking for.
This is only an example:
Hope that helps
Thanks Rob,
I shall give that a go (I never did do it!).
Craig
So I found a way easier way of doing this, more to what you were looking for. Instead of looking in ALL directories under "media", this grabs the media folder by "Name" and then looks in the child list for the file by name. The image path is the first "property" index.
This blog post really helped out: http://jitu-meck.blogspot.com/2013/07/working-with-media-items-in-umbraco-cms.html
is working on a reply...