Is it possible to access the media service from a razor scripting file? If so how and what to include (@using)?
I'm trying to fetch the 5 last modified uploaded media files into a razor view. In another razor view I got it to work to fetch all media files with this code:
Media startMedia = new Media(folderID);
And then just loop with:
@foreach (Media m in startMedia.GetChildMedia())
Since I'm including "umbraco.cms.businesslogic.media" I assume this is the old API? How do I use the new MediaService from the razor view to manage this? I can't get the LINQ-queries to work with the old API.
What version of Umbraco are you using? Are you in MVC-mode, or are you using Razor macroscripts?
If you're using MVC, and your Razor template inherits from UmbracoTemplatePage, you can use the Umbraco-helper to get the media;
// We fetch all the media items at root
// Then we get all its descendants of type "Image",
// order them descending by their createdate, and only take 5 of them
var images = Umbraco.TypedMediaAtRoot()
.SelectMany(x => x.Descendants())
.Where(x => x.DocumentTypeAlias == "Image")
.OrderByDescending(x => x.CreateDate).Take(5);
One note though, this will only get the descendants of type "Image" from the Media root node. If you want to select images from both the root folder, AND its descendandts, I think I would do something like this;
var rootMedia = Umbraco.TypedMediaAtRoot();
var rootImages = rootMedia.Select(x => x)
.Where(x => x.DocumentTypeAlias == "Image");
var descendantImages = rootMedia.SelectMany(x => x.Descendants())
.Where(x => x.DocumentTypeAlias == "Image");
var images = rootImages.Union(descendantImages)
.OrderByDescending(x => x.CreateDate).Take(5);
Hopefully this helps you a bit? If not, feel free to elaborate on some of the questions I gave you earlier :)
I'm using Umbraco v6.1.1 and I'm in WebForms mode (since alot of code is in usercontrols). I tried to inherit from UmbracoTemplatePage but I got the error:
Error occured
Context Must Implement System.Web.WebPages.WebPage
Is it possible to get this to work even thought I'm in WebForms mode? The code seems to solve my issue, but I need to fetch from a specific folderID and I need to fetch all files, including folders.
Yeah, the UmbracoTemplatePage is only available to you in MVC-mode (at least as far as I know). I suppose you can create your own instance of the Umbraco helper in the codebehind of the usercontrol, though.
UmbracoHelper _helper = new UmbracoHelper(UmbracoContext.Current);
Then you can just use it like this;
var images = _helper.TypedMediaAtRoot();
In other words, just replace the Umbraco part from my first answer with _helper.
If you need to fetch from a specific folder, you can just use _helper.TypedMedia(mediaId) instead, and just skip the whole Where(x => x.DocumentTypeAlias == "Image") part of the LINQ.
Hope this helps you to get closer to your solution!
Yes it helps alot, but I wonder if it's possible to solve this issue with only a razor view? Do I need a usercontrol at all? It doesn't matter that much but I'm trying to use razor views when it's possible :) .
Sure, you can do that in a Razor macro as well! I just assumed you were using a usercontrol, as you mentioned that in the previous post.
Note that I haven't tested doing this in a WebForms Umbraco site, but I can't imagine why creating your own instance of the UmbracoHelper inside a Razor macro shouldn't work :)
I'm trying with this code, but I get the error message:
error CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
@{
Umbraco.Web.UmbracoHelper _helper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var folder = _helper.TypedMedia(1083);
var files = folder.Select(x => x);
var descendantFiles = folder.SelectMany(x => x.Descendants());
var result = folder.Union(descendantImages)
.OrderByDescending(x => x.CreateDate).Take(5);
}
Any pointers what I'm doing wrong? I might need to use a user control anyway :) .
Edit; Getting the same error when I'm trying the code in the code behind of a user control.
TypedMedia() only returns a single IPublishedContent, so you don't have to do the whole SelectMany(x => x.Descendants())-thing. That's what the error message is about. IPublishedContent isn't an IEnumerable, thus it doesn't have Select and SelectMany available. The following should work just fine:
Umbraco.Web.UmbracoHelper _helper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var folder = _helper.TypedMedia(1083);
var descendantFiles = folder.Descendants();
var result = descendantFiles.OrderByDescending(x => x.CreateDate).Take(5);
Remember that descendants are all items recursively down the tree, so this will also get all the direct children of the folder, that's why the union of the two result sets are unnecessary.
I suppose the UpdateDate is what you want, yes. I just didn't see one when I tested the initial solution, but that was what you asked for of course. I don't know why the CreateDate has DateTime.MinValue as it's value though..
Ah I found out something that doesn't work. As I said I'm using the function Children instead of Descendants since Descendants dosen't exists for me (?), which means I don't get all files recursively. Is it possible to get all files recursively somehow?
IPublishedContent should have Descendants(), are you sure it's an IPublishedContent you're getting, and that you don't unintentionally cast it to something else, or select some other thing from it? See the image below
MediaService from Razor view
Hello!
Is it possible to access the media service from a razor scripting file? If so how and what to include (@using)?
I'm trying to fetch the 5 last modified uploaded media files into a razor view. In another razor view I got it to work to fetch all media files with this code:
And then just loop with:
Since I'm including "umbraco.cms.businesslogic.media" I assume this is the old API? How do I use the new MediaService from the razor view to manage this? I can't get the LINQ-queries to work with the old API.
What version of Umbraco are you using? Are you in MVC-mode, or are you using Razor macroscripts?
If you're using MVC, and your Razor template inherits from
UmbracoTemplatePage
, you can use theUmbraco
-helper to get the media;One note though, this will only get the descendants of type "Image" from the Media root node. If you want to select images from both the root folder, AND its descendandts, I think I would do something like this;
Hopefully this helps you a bit? If not, feel free to elaborate on some of the questions I gave you earlier :)
Hello and thank you for your answer.
I'm using Umbraco v6.1.1 and I'm in WebForms mode (since alot of code is in usercontrols). I tried to inherit from UmbracoTemplatePage but I got the error:
Error occured
Context Must Implement System.Web.WebPages.WebPage
Is it possible to get this to work even thought I'm in WebForms mode? The code seems to solve my issue, but I need to fetch from a specific folderID and I need to fetch all files, including folders.
Yeah, the
UmbracoTemplatePage
is only available to you in MVC-mode (at least as far as I know). I suppose you can create your own instance of theUmbraco
helper in the codebehind of the usercontrol, though.Then you can just use it like this;
In other words, just replace the
Umbraco
part from my first answer with_helper
.If you need to fetch from a specific folder, you can just use
_helper.TypedMedia(mediaId)
instead, and just skip the wholeWhere(x => x.DocumentTypeAlias == "Image")
part of the LINQ.Hope this helps you to get closer to your solution!
Yes it helps alot, but I wonder if it's possible to solve this issue with only a razor view? Do I need a usercontrol at all? It doesn't matter that much but I'm trying to use razor views when it's possible :) .
Sure, you can do that in a Razor macro as well! I just assumed you were using a usercontrol, as you mentioned that in the previous post.
Note that I haven't tested doing this in a WebForms Umbraco site, but I can't imagine why creating your own instance of the
UmbracoHelper
inside a Razor macro shouldn't work :)I'm trying with this code, but I get the error message:
error CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
@{
Umbraco.Web.UmbracoHelper _helper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var folder = _helper.TypedMedia(1083);
var files = folder.Select(x => x);
var descendantFiles = folder.SelectMany(x => x.Descendants());
var result = folder.Union(descendantImages)
.OrderByDescending(x => x.CreateDate).Take(5);
}
Any pointers what I'm doing wrong? I might need to use a user control anyway :) .
Edit; Getting the same error when I'm trying the code in the code behind of a user control.
TypedMedia()
only returns a singleIPublishedContent
, so you don't have to do the wholeSelectMany(x => x.Descendants())
-thing. That's what the error message is about.IPublishedContent
isn't anIEnumerable
, thus it doesn't haveSelect
andSelectMany
available. The following should work just fine:Remember that descendants are all items recursively down the tree, so this will also get all the direct children of the folder, that's why the union of the two result sets are unnecessary.
Thanks for your help, I'm getting closer..
The function "Descendants" doesn't work for me, currently I'm using "folder.Children" and it seems to be working.
"CreateDate" returns "1/1/0001 12:00:00 AM" for each record, why is that? UpdateDate seems to return the right date (I think).
I suppose the
UpdateDate
is what you want, yes. I just didn't see one when I tested the initial solution, but that was what you asked for of course. I don't know why theCreateDate
hasDateTime.MinValue
as it's value though..Did you manage to make this work?
Yes it works now, a big thank you!
Ah I found out something that doesn't work. As I said I'm using the function Children instead of Descendants since Descendants dosen't exists for me (?), which means I don't get all files recursively. Is it possible to get all files recursively somehow?
IPublishedContent should have Descendants(), are you sure it's an IPublishedContent you're getting, and that you don't unintentionally cast it to something else, or select some other thing from it? See the image below
Yes I'm sure it's an IPublishedContent, see the images below:
As I said I'm using Umbraco v6.1.1, might this be the problem?
is working on a reply...