Copied to clipboard

Flag this post as spam?

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

  • Andrew 21 posts 144 karma points
    Jun 06, 2019 @ 14:24

    Get all Media from a Folder


    I'm trying to get images from a folder I've set up in the Media section.

    I'm using Umbraco.MediaAtRoot() which is returning the root media correctly, however when trying to access the media in a folder (using .Children) I'm getting the following Error:

    'Object Reference not set to an instance of an object'

    The stacktrace is as follows:

    Umbraco.Web.PublishedCache.NuCache.PublishedContent.<>c.<.cctor>b931(IPublishedSnapshot publishedShapshot, Boolean previewing, Int32 id)\r\n at Umbraco.Web.PublishedCache.NuCache.PublishedContent.19.MoveNext()\r\n at System.Linq.Buffer1..ctor(IEnumerable1 source)\r\n at System.Linq.OrderedEnumerable1.<GetEnumerator>d__1.MoveNext()\r\n at System.Linq.SystemCore_EnumerableDebugView1.getItems()

    Does anyone know what is causing this error and how it can be resolved? Or is there a better way to achieve what I am trying to do? It feels like something that should be fairly simple so not sure if I'm missing something ...


  • Ifrahim Rasool 27 posts 81 karma points
    Jun 06, 2019 @ 14:53
    Ifrahim Rasool

    Why dont you create instance of MediaService and se GetRootMedia() function to get your Media folder by name and use .Children to fetch all images.

    Something like:

            var _mediaService = ApplicationContext.Services.MediaService;
            var _mediaFolder = (IPublishedContent)_mediaService.GetRootMedia().FirstOrDefault(x => x.Name.InvariantEquals("MyImages"));

    Hope that helps.

  • Rhys Hamilton 140 posts 940 karma points
    Jun 06, 2019 @ 15:29
    Rhys Hamilton

    Hi Andrew,

    Could you share the full code that you're using to retrieve your folder? It might give a better understanding of what's going wrong.

    Similarly, you can also see my solution here in how to retrieve children from a media folder (using a media picker in V8).

Please Sign in or register to post replies

Write your reply to: