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 ...
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"));
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:
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.Buffer
1..ctor(IEnumerable
1 source)\r\n at System.Linq.OrderedEnumerable1.<GetEnumerator>d__1.MoveNext()\r\n at System.Linq.SystemCore_EnumerableDebugView
1.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,
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:
Hope that helps.
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).
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.
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:
is working on a reply...