Copied to clipboard

Flag this post as spam?

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


  • Andrew 25 posts 148 karma points
    Jun 06, 2019 @ 14:24
    Andrew
    1

    Get all Media from a Folder

    Hi,

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

    Thanks,

  • Ifrahim Rasool 28 posts 84 karma points
    Jun 06, 2019 @ 14:53
    Ifrahim Rasool
    1

    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 942 karma points
    Jun 06, 2019 @ 15:29
    Rhys Hamilton
    0

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

  • David Armitage 508 posts 2078 karma points
    Jul 13, 2021 @ 06:39
    David Armitage
    1

    Hi,

    Here is some code that I use to get a child media folder. I think you will find this useful.

    I am not sure if it is the most efficient way but it works for me.

    I tried to go down the Lucene path but didn't get very far.

     public IMedia GetMediaFolder(int parentFolderId, string folderName)
            {
                try
                {
                    IMedia parentFolder = _mediaService.GetById(parentFolderId); ;
                    if(parentFolder != null)
                        return _mediaService.GetPagedChildren(parentFolder.Id, 0, int.MaxValue, out long total).FirstOrDefault(x => x.Name == folderName);
                }
                catch (Exception e)
                {
                    _logger.Error<UmbMediaService>("GetMediaFolder | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                }
    
                return null;
            }
    
  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Jul 13, 2021 @ 07:10
    Patrick de Mooij
    2

    Quick note about this logic. While this will work, it'll also do database hits due to using the MediaService. So this is not recommended if you are doing this a lot.

    If you don't need to have the IMedia item, I suggest getting it from the Umbraco Cache:

    public IPublishedContent GetMediaFolder(int parentFolderId, string folderName)
        {
            try
            {
                var parentFolder = Umbraco.Media(parentFolderId);
                if (parentFolder != null)
                    return parentFolder.Children().FirstOrDefault(x => x.Name == folderName);
            }
            catch (Exception e)
            {
                _logger.Error<UmbMediaService>("GetMediaFolder | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
            }
    
            return null;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft