Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Nov 14, 2013 @ 22:23
    Craig O'Mahony
    0

    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

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 14, 2013 @ 22:46
  • Craig O'Mahony 364 posts 918 karma points
    Nov 15, 2013 @ 15:19
    Craig O'Mahony
    0

    Thanks Dennis,

    I shall give this a try and let you know how I go.

    Thanks again :)

  • Charles Afford 1163 posts 1709 karma points
    Nov 16, 2013 @ 18:51
    Charles Afford
    0

    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

    }

  • Craig O'Mahony 364 posts 918 karma points
    Nov 18, 2013 @ 14:57
    Craig O'Mahony
    0

    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!!

  • Rob Scott 41 posts 94 karma points
    Nov 05, 2014 @ 21:32
    Rob Scott
    0

    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);
            }
        }
    

    Hope that helps

  • Craig O'Mahony 364 posts 918 karma points
    Nov 06, 2014 @ 09:58
    Craig O'Mahony
    0

    Thanks Rob,

    I shall give that a go (I never did do it!).

    Craig

  • Rob Scott 41 posts 94 karma points
    Nov 06, 2014 @ 16:27
    Rob Scott
    0

    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

    /// <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 = string.Empty;
    
        var mediaFolder = UmbracoContext.Current.Application.Services.MediaService.GetRootMedia().FirstOrDefault(c => c.Name == "Swatches");
        if (mediaFolder != null)
        {
            swatch = UmbracoContext.Current.Application.Services.MediaService.GetChildren(mediaFolder.Id)
                                   .FirstOrDefault(x => x.Name.ToLower().Equals(string.Format("swatch_{0}.jpg", color.ToLower())))
                                   .Properties.First().Value.ToString();
        }
    
        return !string.IsNullOrEmpty(swatch) ? swatch : string.Format("{0}swatch_{1}.jpg", swatchPath, color);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft