Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert J. Bullock 325 karma points
    Mar 16, 2011 @ 18:13
    Robert J. Bullock
    0

    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.

  • Sebastiaan Janssen9133 karma points admin mvp hq
    Mar 16, 2011 @ 19:57
    Sebastiaan Janssen
    0

    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.

  • Hendy Racher2791 karma points admin core
    Mar 16, 2011 @ 20:03
    Hendy Racher
    0

    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

  • Jonas Eriksson1785 karma points
    Mar 16, 2011 @ 20:23
    Jonas Eriksson
    1

    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.

  • Robert J. Bullock325 karma points
    Mar 16, 2011 @ 21:19
    Robert J. Bullock
    0

    If I were to use uQuery.GetChildMedia(), how do I specify the start folder? It doesn't seem to take any parameters.

  • Robert J. Bullock325 karma points
    Mar 16, 2011 @ 21:37
    Robert J. Bullock
    0

    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.

  • Robert J. Bullock325 karma points
    Mar 16, 2011 @ 21:41
    Robert J. Bullock
    0

    FYI, that "file browser" sample doesn't work at all for me.

  • Hendy Racher2791 karma points admin core
    Mar 16, 2011 @ 21:43
    Hendy Racher
    0

    Hi Robert,

    The GetChildMedia() method is an extension method on the umbraco.cms.businesslogic.media.Media object, so you can do the following:

    using umbraco.cms.businesslogic.media;

    Media startMedia = new Media(123);

    foreach(Media childMedia in startMedia.GetChildMedia())
    {
    ...
    }

    HTH,

    Hendy

  • Robert J. Bullock325 karma points
    Mar 16, 2011 @ 22:01
    Robert J. Bullock
    2

    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 in startMedia.GetChildMedia())
    {
        <img src="/imagegen.ashx?image=@m.getProperty("umbracoFile").Value&width=250"/>;
     }
    }

  • Jonas Eriksson1785 karma points
    Mar 17, 2011 @ 05:57
    Jonas Eriksson
    0

    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!

  • Ben Norman211 karma points
    Nov 03, 2011 @ 10:53
    Ben Norman
    0

    What is the link for the wiki sample mentioned above?

  • Jonas Eriksson1785 karma points
    Nov 03, 2011 @ 11:08
    Jonas Eriksson
    0

    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

  • Amir Khan1076 karma points
    Nov 29, 2011 @ 20:56
    Amir Khan
    0

    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

  • Amir Khan1076 karma points
    Nov 29, 2011 @ 21:40
    Amir Khan
    0

    Alright, have some progress. Anyone know if there is a more approrpriate way to do this?

     

    <h2>@Model.Name</h2>

    @using umbraco.cms.businesslogic.media;
    @using uComponents.Core;
    @using uComponents.Core.uQueryExtensions;




    @if (Model.HasProperty("imageFolder") && Model.imageFolder != 0){
    var startFolder = Model.imageFolder;
    Media startMedia = new Media(startFolder);
    foreach(Media myMedia in startMedia.GetChildMedia())
    {
    <img src="@myMedia.getProperty("umbracoFile").Value" width="@myMedia.getProperty("umbracoWidth").Value" height="@myMedia.getProperty("umbracoHeight").Value"/>
    }


    }
  • Ben Norman211 karma points
    Nov 29, 2011 @ 22:23
  • Amir Khan1076 karma points
    Nov 30, 2011 @ 16:05
    Amir Khan
    0

    Hi Ben, thanks for the response. Why is that setup better? Sorry if that's a stupid question...

  • Ben Norman211 karma points
    Feb 10, 2012 @ 23:25
  • Idan23 karma points
    Dec 26, 2012 @ 09:29
    Idan
    0

    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

    }

Please Sign in or register to post replies

Write your reply to:

Draft
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community.