Copied to clipboard

Flag this post as spam?

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


  • greengiant83 88 posts 109 karma points
    Aug 18, 2011 @ 00:42
    greengiant83
    0

    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?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 18, 2011 @ 17:01
    Dan Diplo
    0

    Something like this should work:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    
    
    @{
        int mediaFolderId = 12345;
    
        DynamicMedia folder = new DynamicMedia(mediaFolderId);
    
        if (folder != null && folder.Children.Items.Count() > 0)
        {
            <ul>
                @foreach (dynamic media in folder.Children.Items)
            {
                    if (media.ContentType.Alias == "Image")
                    {
                        <li>
                            <img src="@media.umbracoFile" width="@media.umbracoWidth" 
                                height="@media.umbracoHeight" alt="@media.Name" />
                        </li>
                    }
            }
            </ul>   
        }                             
    }

    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:

    int mediaFolderId = Model.mediaFolder;

    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.

  • Barry Fogarty 493 posts 1129 karma points
    Sep 01, 2011 @ 19:52
    Barry Fogarty
    0

    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?

  • Barry Fogarty 493 posts 1129 karma points
    Sep 02, 2011 @ 00:32
    Barry Fogarty
    1

    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>
        }
     
    }

  • Tom 161 posts 322 karma points
    Mar 25, 2015 @ 17:48
    Tom
    0

    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?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 25, 2015 @ 21:07
    Dan Diplo
    1

    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");

    That should get you started.

  • Tom 161 posts 322 karma points
    Mar 26, 2015 @ 18:16
    Tom
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 26, 2015 @ 21:00
    Dan Diplo
    1

    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.

     

     

  • Tom 161 posts 322 karma points
    Mar 27, 2015 @ 12:55
    Tom
    0

    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

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 28, 2015 @ 16:18
    Dan Diplo
    0

    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...

    @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:
    <ul>
        @foreach (var article in Model.Content.Children())
        {
            <li><a href="@article.Url">@article.Name</a></li>
        }
    </ul>

    Alternatively, if you wanted to access the News page and children from a different page you could go  (remembering it's Id is 1800)

    <ul>
        @foreach (var article in Umbraco.TypedContent(1800).Children())
        {
            <li><a href="@article.Url">@article.Name</a></li>
        }
    </ul>
    Hope that helps a bit?
Please Sign in or register to post replies

Write your reply to:

Draft