Search code examples
umbracoumbraco7

Get Media folder by name or hash in Umbraco 7


I'm using Umbraco 7.4.

I'd like to enumerate all files from a Media folder ("Images\Splash").

To a custom class, I pass the reference of the UmbracoHelper instance from the controller, and can get the folder like this:

var folder = _umbraco.Media(333);

333 being the Id of the Media folder I want.

How can I get this folder by it's hash, or by it's name?
I'd like to not use the Id, as this is not guaranteed to be the same between environments (dev, staging, production).


Solution

  • As it turns out, you can get a media folder by name, using the UmbracoHelper (_umbraco):

                var mediaService = _umbraco.UmbracoContext.Application.Services.MediaService;
                var imagesFolder = (IPublishedContent)mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals("Images"));
                var folder = imagesFolder?.Children().FirstOrDefault(c => c.Name.InvariantEquals("Splash"));