Copied to clipboard

Flag this post as spam?

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


  • Anton 135 posts 186 karma points
    Jan 12, 2012 @ 03:42
    Anton
    0

    Get All Images in Media Gallery?

    I have such structure in media

    Media(folder)

    --Sites(folder)

    ----Projects(folder)

    ---------ImageOfProject(image)

    ----Projects(folder)

    ---------ImageOfProject(image)

    How can I get all images of all projects?

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 12, 2012 @ 08:52
    Dirk De Grave
    1

    HI Anton,

    you could use umbraco.library.GetMedia(mediaId, true) to fetch all images from a specific 'Projects' folder. From xslt, you'd use umbraco.library:GetMedia() (mind the semicolon instead of just a dot) whereas in c# code, you just use umbraco.library.GetMedia().

    First paramter is the id of the 'Projects' folder, second parameter (true) tells function to retrieve all images below the 'Projects' folder (may be including the 'Projects' folder media item as well...)

    Cheers,

    /Dirk

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 12, 2012 @ 09:43
    Jeroen Breuer
    0

    Dirk is right, but the problem is that this won't work for the root folder (with id -1). You can't use that in umbraco.library.GetMedia. Here is a solution which I used in C# (which probably will also work in Razor).

    protected static umbraco.DataLayer.ISqlHelper SqlHelper
    {
        get 
        {
            return umbraco.BusinessLogic.Application.SqlHelper; 
        }
    }

    public System.Xml.Linq.XDocument GetMediaXML(int mediaId) { //Create an empty xmlDocument. System.Xml.Linq.XDocument xmlDocument = new System.Xml.Linq.XDocument( new System.Xml.Linq.XElement("Media") ); if (mediaId == -1) { //If the mediaId is -1 loop through all the top media nodes. using (umbraco.DataLayer.IRecordsReader dr = SqlHelper.ExecuteReader("Select id from umbracoNode where nodeObjectType = @type And parentId = -1 order by sortOrder", SqlHelper.CreateParameter("@type", umbraco.cms.businesslogic.media.Media._objectType))) { while (dr.Read()) { //Get the hierarchical xml from each topnode and add it to the xmlDocument. xmlDocument.Root.Add(System.Xml.Linq.XElement.Parse(umbraco.library.GetMedia(dr.GetInt("id"), true).Current.OuterXml)); } } } else { //Get the hierarchical xml from the media id and add it to the xmlDocument. xmlDocument.Root.Add(System.Xml.Linq.XElement.Parse(umbraco.library.GetMedia(mediaId, true).Current.OuterXml)); } return xmlDocument; }

    Jeroen

  • aciamge 2 posts 22 karma points
    May 20, 2013 @ 03:48
    aciamge
    0

    c# image library may help you with that, and the codes here is playing well. thank you for helping.

Please Sign in or register to post replies

Write your reply to:

Draft