Copied to clipboard

Flag this post as spam?

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


  • Gitte 1 post 71 karma points
    Sep 24, 2015 @ 08:42
    Gitte
    0

    Referencing the root media folder

    Hi, I was wondering if anyone could help me with this:

    How can I loop through all children in the root media folder? Trying to do this with Razor in Umbraco 7.1.4

    My problem is I need some way to find media files without using their IDs. As far as I can tell you can't get nodes directly by name, so the best solution I can think of is to traverse down through the media folder tree and find the files that way.

    Now I just need to figure out how to access the root folder.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 29, 2015 @ 20:53
    Alex Skrypnyk
    0

    Hi Gitte,

    Do you have some progress with your issue? Why do you need to get media by name?

  • Artyom Chernenko 17 posts 91 karma points
    Sep 30, 2015 @ 04:50
    Artyom Chernenko
    0

    Hi Gittie

    UmbracoHelper has method TypedMediaAtRoot() returning IEnumerable of IPublishedContent - this will be the list of medias under the media root. Then you are able either to traverse media hierarchy or to call extension method FlattenList(media => media.Children) on IEnumerable

  • Artyom Chernenko 17 posts 91 karma points
    Sep 30, 2015 @ 05:02
    Artyom Chernenko
    1

    Just for ref - here is an extension method I implemented to get a media URL by media 'path'. You can adapt it for your needs.

    public static string GetMediaUrlByPath(this UmbracoHelper umbraco, string path)
        {
            if (path.IsNullOrWhiteSpace())
            {
                return null;
            }
            var p = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var currentLevelList = ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem<IEnumerable<IPublishedContent>>("Media-Root", umbraco.TypedMediaAtRoot, TimeSpan.FromHours(24));
            for(var i = 0; i < p.Length; i++)
            {
                var media = p[i];
                if (i == p.Length - 1)
                {
                    return currentLevelList.FirstOrDefault(m => m.Name == media).NotNull(m => m.Url);
                }
                currentLevelList = currentLevelList.FirstOrDefault(m => m.Name == media).NotNull(m => m.DocumentTypeAlias == Constants.Conventions.MediaTypes.Folder ? m.Children() : null);
                if (currentLevelList == null)
                {
                    LogHelper.Warn(typeof(MediaExtensions), "Unable to get media by path '{0}': '{1}' media does not exist", () => path, () => media);
                    break;
                }
            }
            return null;
        }
    

    For example, calling this method with argument 'Main Site Images/Locations/Brazil/Brazil_1.jpg' will return URL of the image you see on the screenshot

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft