2

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).

Attila Szasz
  • 3,033
  • 3
  • 25
  • 39

3 Answers3

8

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"));
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
Attila Szasz
  • 3,033
  • 3
  • 25
  • 39
0

I don't think you can :-/ But you could make it a property somewhere on the site (or a parameter on a macro) so you wouldn't have to hard code the ID anywhere.

Jannik Anker
  • 3,320
  • 1
  • 13
  • 21
0

You can't get a media item by name using the Umbraco Helpers.

The way I go about this is by using Web.config Transformations, to set the different Node Id's for each environment (i.e. Local, Staging & Live etc.).

<add key="HomeNodeId" value="xxxx" />

Then create a class for calling these different NodeId's.

See the following article: https://weblogs.asp.net/srkirkland/common-web-config-transformations-with-visual-studio-2010

Good luck

Craig

Craig Mayers
  • 185
  • 2
  • 9