Copied to clipboard

Flag this post as spam?

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


  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 02:18
    Mike
    0

    (Razor) Looping through images in a Media folder - can't reference umbracoFile

    Hi y'all,

    Ok, this is steadily driving me insane and I've no idea why this isn't working. I've written some Razor which happily checks a media picker (alias "kerbImagesGallery") to see if any images are stored within the folder and if so loops through them.

    @{
    dynamic mediaFolderId = Model.kerbsImageGallery;
    DynamicMedia folder = new DynamicMedia(mediaFolderId);

    if (folder != null && folder.Children.Items.Count() > 0)
    {
    foreach (dynamic media in folder.Children.Items)
    {
    <p><a href="@media.umbracoFile" class="fancybox" rel="kerbs" title="@media.altTag">
    <img src="@media.Name" alt="@media.altTag" title="@media.altTag">
    </a></p>
    }
    }
    }

    I can happily reference each item name (@media.Name), and also a Alt Text custom property I added to the media datatype (@media.altTag) BUT for some reason I cannot reference the image path (@media.umbracoFile) or indeed any of the other values such as width, height, size etc. 

    If I reference @media.Id I get the ID value of each image item within that folder so why can't I get the path? 

    Thanks vm

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 08:33
    Jeavon Leopold
    0

    Maybe the conversion from DynamicMedia to dynamic isn't quite right, have you tried using var folder = Library.MediaById(mediaFolderId) instead as that will return a dynamic object?

  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 13:34
    Mike
    0

    Hi Jeavon, I did try this. I'm getting a conflict on the count reference though:

    Non-invocable member 'System.Collections.Generic.List<umbraco.MacroEngines.DynamicNode>.Count' cannot be used like a method.
  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 13:37
    Mike
    0

    I've removed the count() reference by way of a quick test and no errors but the @media.umbracoFile value is still returned as empty.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 14:12
    Jeavon Leopold
    100

    Hi Mike,

    I just gave this snippet a go on v6.1.6 and it worked fine:

    @{
       var mediaFolderId = Model.kerbsImageGallery;
       var folder = Library.MediaById(mediaFolderId);
    
       if (folder != null && folder.Children.Count() > 0)
        {
          foreach (var media in folder.Children)
           {
              <p><a href="@media.umbracoFile" class="fancybox" rel="kerbs" title="@media.altTag">
                    <img src="@media.umbracoFile" alt="@media.altTag" title="@media.altTag">
                </a></p>
           }
      }
    }
    

    Maybe try that, if it doesn't work, what version of Umbraco are you using?

    Jeavon

  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 14:56
    Mike
    0

    Hi there Jeavon,

    I reworked a few things and pretty much got the exact same code, I added a few bits in to just test the output which all work fine. Everything work but still @media.umbracoFile returns nothing.

    @{
                    var mediaFolderId = Model.kerbsImageGallery;
                    var folder = Library.MediaById(mediaFolderId);
                   
                    if (folder != null && folder.Children.Count() > 0)
                    {
                        <p style="color:cyan">@folder.Children.Count()</p>
                        <p style="color:red">@mediaFolderId</p>
                       
                        foreach (var media in folder.Children)
                        {
                            <a href="@media.umbracoFile" class="fancybox" rel="kerbs" title="@media.altTag">
                                <img src="@media.Name" alt="@media.altTag" title="@media.altTag">
                            </a>
                        }
                    }
                }

    I am also using v6.1.6

    Just tried cutting/pasting your code in case I had missed something really silly, like a typo, but nope! :(    Just makes no sense.

  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 15:16
    Mike
    0

    Just for the sake of trying something new I just removed one of the files from the folder and reuploaded it to Media - now it is working!!!!   *facepalm*

    Just can't think why this would happen - bad initial upload maybe?  Anyway the code you supplied was good - thanks Jeavon.

    Could I be cheaky and ask one further question. Do you know how I'd go about adding a class to all but the very first image it loops through?  So as an example when rendered it would look like:

    <p><a href="pathToFile1.jpg" class="fancybox" rel="..."><img...></a></p>
    <p class="hidden"><a href="pathToFile2.jpg" class="fancybox" rel="..."><img...></a></p>
    <p class="hidden"><a href="pathToFile3.jpg" class="fancybox" rel="..."><img...></a></p>
    etc...

    :-)

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 15:23
    Jeavon Leopold
    0

    Maybe, or could possibly be a corrupted Examine Index, at least it's working now!

    Anyway to answer your question:

    @{
       var mediaFolderId = Model.kerbsImageGallery;
        var folder = Library.MediaById(mediaFolderId);
    
       if (folder != null && folder.Children.Count() > 0)
        {
          foreach (var media in folder.Children)
           {          
              <p @Html.Raw(!media.IsFirst() ? "class=\"hidden\"": string.Empty)><a href="@media.umbracoFile" class="fancybox" rel="kerbs" title="@media.altTag">
                    <img src="@media.umbracoFile" alt="@media.altTag" title="@media.altTag">
                </a></p>
           }
      }
    }
    
  • Mike 62 posts 274 karma points
    Dec 18, 2013 @ 15:39
    Mike
    0

    Works like a charm! Thanks for all your help sorting this one Jeavon.  I've marked your previous reply as the solution as that was the topic at hand, but very much appreciate the extra item here. Thanks again! :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 18, 2013 @ 15:40
    Jeavon Leopold
    0

    You're very welcome! :)

Please Sign in or register to post replies

Write your reply to:

Draft