Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Get a specific image from the global assets and display it in a view



From what I understand on this page:

EPiServer stores all its assets using blog storage. I am trying to get a bit more understanding of how this works and how to access it.

Now in the default MVC Alloy Sample I have a controller and i want to load an image that is located (uploaded) under [Media]->'For this site'->'Alloy Meet' (AlloyMeet.png) and display this image in a view. I want to accomplish this using the BlogFactory.

From what i found out I have to do something like: BlobFactory.Instance.GetBlob() and I can find the root asset folder using SiteDefinition.Current.GlobalAssetsRoot.

But now how do I get this image, put its contentReference in a ViewModel property that i then pass to the view where I display the image. I know there are easier ways to do this but I like to know how to do it using the BlobFactory or something similair.


Oct 20, 2014 17:15

In 7.5, all media items are content items, in the same way as Pages or Blocks.

Therefore, you can locate specific media using an IContentRepository in the same way as Pages or Blocks, using the GlobalAssetsRoot as a starting content reference.

BlobFactory is for getting or creating the actual blob data associated with the media content item. I'm not sure why you specifically want to use BlobFactory, as this would not help in this instance.

Oct 20, 2014 18:06

Thanks Simon for your response. You helped me a lot already.

For looping through the media assets I now use:

var assets = contentRepository.GetChildren<MediaData>(SiteDefinition.Current.GlobalAssetsRoot);

foreach (var item in assets)
      // Do something

But this wont loop through the subfolders in the GlobalAssetsRoot. How do I accomplish that?

For example in the Alloy Sample Page you have subfolders in the media panel like

  • Alloy Meet
  • Alloy Plan
  • Alloy Track
  • etc

With the above code I only loop through the media that is located in the root itself.

Oct 21, 2014 11:36

If you are interested in the subfolders sitting under the root folder, you can get to them like this:

var rootFolders = ContentRepository.GetChildren<ContentFolder>(SiteDefinition.Current.GlobalAssetsRoot);

            foreach (var rootFolder in rootFolders)
                // Do something

Oct 21, 2014 11:45

Is starting with the GlobalAssetsRoot and looping through folders the most efficient way to find a folder several levels deep?

Jul 27, 2016 23:40

Normally you don't search for them no. You create a new property (ContentReference) on the page/block that uses the image and let editor set that. Then you use that to find your media instead of searching through media files for a file with a specific name. For layout images I normally store content references on startpage.

You can of course loop through them all as well but it's rare that you really need to. Then you can use UrlResolver.GetUrl to get the url to the file of you need it.

Jul 28, 2016 10:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.