Copied to clipboard

Flag this post as spam?

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


  • Tony Kiernan 278 posts 341 karma points
    Sep 30, 2011 @ 11:36
    Tony Kiernan
    0

    Random image from gallery

    I am currently pulling the first image from a gallery folder using

    Media gallery = new Media(media_id);
    dynamic pic = gallery.Children.First();

    How could I do something like this

    dynamic pic = gallery.Children.Random();
  • Kristoffer Eriksen 185 posts 465 karma points
    Sep 30, 2011 @ 11:44
    Kristoffer Eriksen
    0

    If you get the image gallery folder, you can get a random image with ImageGen.

    http://our.umbraco.org/projects/website-utilities/imagegen

    page 16 in the Documentation PDF.

  • Tony Kiernan 278 posts 341 karma points
    Oct 03, 2011 @ 12:14
    Tony Kiernan
    0

    Nice.  Now, how do I get the folder? I'd assume something like

    string str_folder = gallery.getProperty("Url").Value.ToString();

    or

    string str_folder = Model.NodeById(gallery_id).Url;

    Neither of which is right

  • Kristoffer Eriksen 185 posts 465 karma points
    Oct 03, 2011 @ 12:24
    Kristoffer Eriksen
    0

    Hey Tony

    Well that depends on your solution, and how you created your gallery.

    I just assumed, that since you wrote "from a gallery folder using" you already had the path for your gallery.

    Are you using some sort of gallery package ?

     

  • Tony Kiernan 278 posts 341 karma points
    Oct 03, 2011 @ 22:21
    Tony Kiernan
    0

    Sorr, my bad.  Read that as "from a media folder".  The only package being used is

    umbraco.cms.businesslogic.media
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 03, 2011 @ 22:32
    Dan Diplo
    1

    In Umbraco 4.7.1 you can easily get a random image from a folder in the media library. Assuming you have the id of your folder:

    dynamic folder = Library.MediaById(1054);
    var randomImage = folder.Children.Where("nodeTypeAlias = \"Image\"").Random();
    
    In my example 1054 is the id of the folder containing the images. The where part is only needed if your folder can potentially contain non-image files.

     

Please Sign in or register to post replies

Write your reply to:

Draft