Hey Dan, nice code thanks for sharing! Any idea how I would order the returned list? I have a custom media type with a Date property, and I have tried:
My original answer was for Umbraco 4. Things have moved on a lot since then. You can use strongly typed queries in later versions of Umbraco 6 and Umbraco 7.
So you could get media by Id (as IPublishedContent) by using:
var media = Umbraco.TypedMedia(123);
If you don't want to hard-code an Id you could also iterate through Media using a LINQ query. So you'd start from the root node and then search for media of type. For instance, if you wanted to find the first instance of a folder with the name "MyFolderName" you could do:
var folder = Umbraco.TypedMediaAtRoot().DescendantsOrSelf("Folder").Where(x => x.Name == "MyFolderName").FirstOrDefault();
Note that if you have a lot of Media this might not be very efficient.
You can get the children of a folder like this (where I'm just getting Images):
var images = folder.Children(x => x.DocumentTypeAlias == "Image");
Umbraco is a helper utlity class whereas Model.Content is a represenation of the data in your document type for a given page. So they're two different things.
Umbraco is just a helper utility for getting back content and media. It can return this as either strongly typed instance of IPublishedContent OR it can return it dynamically typed. So...
@Umbraco.TypedContent(123) // returns a strongly typed instance of IpublishedContent
@Umbraco.Content(123) // returns the same content, but as a dynamic type
So the methods with the "Typed" prefix return strongly typed instances.
Model.Content is a single instance of IPublishedContent. This is available in every view (and relates to the document type mapped to that view/template) or by using the @Umbraco.Typed... methods to get a different instance.
So once you have an instance of Model.Content then you can call the differnet methods for traversing the tree.
So, for instance, say you have a your content tree like this:
Home
-- News (id 1800)
----- News Article One
----- News Article Two
----- News Article Three
So you have a News page (id 1800) with News Articles beneath it as child pages.
If you go to the News page then in your view you will have access to @Model.Content which is a represenation of that news page. It has all the properties you defined in your News document type. But you can also access all the child News Articles from it by accessing @Model.Content.Children(), which gives you access to all the child pages of the News page. So you could list them all like this:
How to get list of images in a folder using Razor
My content item has a property called mediaFolder that contains the id of a media folder. Using razor how can I get a list of images in that folder?
Something like this should work:
Note this uses a hard-coded mediaFolderId of 12345. Obviously this needs to be the Id of your folder containing the images.
If you wanted to populate it using a property called mediaFolder on your current document then you should be able to do this:
Note: You may need to do some checking to ensure your Model does have such a property. You can use the Model.HasProperty() method for that.
Hey Dan, nice code thanks for sharing! Any idea how I would order the returned list? I have a custom media type with a Date property, and I have tried:
folder.Children.Items.OrderByDescending("date")
folder.Children.Items.OrderByDescending(x => x.getProperty("date").Value)
But with no success as yet. Are media items sortable just like regular nodes?
For reference I solved this by using the excellent uQuery extensions from uComponents:
@using umbraco.cms.businesslogic.media;
@using uComponents.Core;
@using uComponents.Core.uQueryExtensions;
@{
Media startMedia = new Media(1148);
foreach(Media m in startMedia.GetChildMedia().OrderByDescending(x => x.getProperty("date").Value))
{
<p>@m.getProperty("date").Value :: @m.getProperty("umbracoFile").Value</p>
}
}
Is there any way to strongly type it?
Also if I don't want to hard code in the medialfolderid (int mediaFolderId =12345) how would I iterate through to get the right one?
My original answer was for Umbraco 4. Things have moved on a lot since then. You can use strongly typed queries in later versions of Umbraco 6 and Umbraco 7.
So you could get media by Id (as IPublishedContent) by using:
If you don't want to hard-code an Id you could also iterate through Media using a LINQ query. So you'd start from the root node and then search for media of type. For instance, if you wanted to find the first instance of a folder with the name "MyFolderName" you could do:
Note that if you have a lot of Media this might not be very efficient.
You can get the children of a folder like this (where I'm just getting Images):
That should get you started.
How did you know when to use Umbraco -vs- Model.Content?
For me Model.Content, did not return an object for TypedMedia.
And I go no intellisense when I used Umbraco.
Tom
Umbraco is a helper utlity class whereas Model.Content is a represenation of the data in your document type for a given page. So they're two different things.
Check out the docs:
https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
https://our.umbraco.org/documentation/Reference/Templating/Mvc/querying
You should get intellisense in cshtml Views when using Model.Content (IpublishedContent) as opposed to dynamics.
Dan:
Thanks so much for replying.
If I understand the links correctly, Umbraco is a unified way to work with published content/media and its dynamic.
But isn't Model.Content a way to work with content/media?
If that's true, when do you know which one to use?
Thanks
Tom
Tom,
Umbraco is just a helper utility for getting back content and media. It can return this as either strongly typed instance of IPublishedContent OR it can return it dynamically typed. So...
So the methods with the "Typed" prefix return strongly typed instances.
Model.Content is a single instance of IPublishedContent. This is available in every view (and relates to the document type mapped to that view/template) or by using the @Umbraco.Typed... methods to get a different instance.
So once you have an instance of Model.Content then you can call the differnet methods for traversing the tree.
So, for instance, say you have a your content tree like this:
Alternatively, if you wanted to access the News page and children from a different page you could go (remembering it's Id is 1800)
is working on a reply...