The wiki sample had a missing { } (which worked with 4.6) sorry for that.
I updated the wiki sample now and changed the extra "get safe property" to GetPropertyAsString from uQuery instead. Looks much better now.
About number of samples - this is still fresh so yes, not that many, but the count is increasing day by day :), in the forum, in the wiki (which everyone is free to add to), and don't forget the included razor macro templates.
Loop Over Media Folder With Razor?
How the heck do I just get a media folder and loop over it's contents with Razor? Can't find any examples for this.
According to this other topic, Media.Children hasn't been implemented because media isn't cached.
The workaround would be to do a good old fashioned umbraco.library.GetMedia(mediaId, true) and work from there.
Hi, or if you're using uComponents how about the uQuery methods to access collections of Media ?
There are the usual XPath axis type extension methods that can be used with Linq expressions:
GetAncestorMedia()
GetAncestorOrSelfMedia()
GetSiblingMedia()
GetDescendantMedia()
GetDescendantOrSelfMedia()
GetChildMedia()
as well as static helpers:
GetMediaByXPath(string)
GetMediaByCsv(string)
GetMediaByXml(string)
GetMediaByName(string)
GetMediaByType(string)
HTH,
Hendy
Here's a sample "file browser" http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/simple-media-browser . However using uComponents would make the code nicer.
If I were to use uQuery.GetChildMedia(), how do I specify the start folder? It doesn't seem to take any parameters.
I get this error when trying to loop over the collection:
: error CS0117: 'umbraco.item' does not contain a definition for 'Image'.
This is confusing... Razor looks cool, but not enough good examples out there yet to be productive with it.
FYI, that "file browser" sample doesn't work at all for me.
Hi Robert,
The GetChildMedia() method is an extension method on the umbraco.cms.businesslogic.media.Media object, so you can do the following:
HTH,
Hendy
That was hard... Here's what ultimately worked for me...
@using umbraco.cms.businesslogic.media;
@using uComponents.Core;
@using uComponents.Core.uQueryExtensions;
@{
Media startMedia = new Media(1102);
foreach(Media m in startMedia.GetChildMedia())
{
<img src="/[email protected]("umbracoFile").Value&width=250"/>;
}
}
Nice!
The wiki sample had a missing { } (which worked with 4.6) sorry for that.
I updated the wiki sample now and changed the extra "get safe property" to GetPropertyAsString from uQuery instead. Looks much better now.
About number of samples - this is still fresh so yes, not that many, but the count is increasing day by day :), in the forum, in the wiki (which everyone is free to add to), and don't forget the included razor macro templates.
Happy coding!
What is the link for the wiki sample mentioned above?
This one http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/simple-media-browser - but that sample could / should be rewritten some now for version 4.71
How would I go about pulling "startMedia" from a document property? Something like "Media startMedia = Model.imageFolder"?
Here's my code:
@if (Model.HasProperty("imageFolder") && Model.imageFolder != 0){
Media startMedia = new Media(1082);
foreach(Media childMedia in startMedia.GetChildMedia())
{
<p>test</p>
}
}
Thanks!
Amir
Alright, have some progress. Anyone know if there is a more approrpriate way to do this?
Dude, Feast your eyes on this http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/simple-media-browser/media-folder-content-list
Hi Ben, thanks for the response. Why is that setup better? Sorry if that's a stupid question...
http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/simple-media-browser
Hi ,
i've tried the code above with no luck ...
I have a media folder with pictures under it .
I used media picker prop to chose that folder from my Home contet tab. ....
but im keep getting empty image tag.
media picker alias : mediaFiles
the code :
@inherits umbraco.MacroEngines.DynamicNodeContext
@if (Model.HasProperty("mediaFiles"))
{
Simple:
Advanced:
@{
var image = @Model.Media("mediaFiles");
}
} else {
This page doesn't contain a MediaPicker property with the alias of 'mediaFiles'
or No image is selected on this page
}
is working on a reply...