Copied to clipboard

Flag this post as spam?

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


  • Andreas Kristensen 20 posts 79 karma points
    May 20, 2013 @ 13:34
    Andreas Kristensen
    0

    List Files from MediaFolder

    Apologies for this post. i can see a lot of similar questions but none of the posts are helping me.

    I have a mediafolder with a couple of pdf files and an image, just for testing.
    All i want is a list of all the files in the mediafolder(or just the pdf's), but i can only get out images. 
    Tried with node, umbracoFile, data, ect ect. nothing comes up.
    However, if i change /node to /Image it shows a link to the image.

     <xsl:template match="DKV_filegal">
           
      <xsl:variable name="mediaFolderId" select="number(mediaFolderId)" />
       <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($mediaFolderId, 'true')" />   
          <xsl:for-each select="$mediaFolder/node">
            <li>
                    <href="{current()/data [@alias='umbracoFile']}">
                            <xsl:value-of select="current()/@nodeName" />
                    </a>
            </li>
          </xsl:for-each>
          
        </xsl:template

    Researching keeps leading me to a gamut of sites saying that getmedia is broken, or that getmedia works just like i have tried unsuccesfully.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 20, 2013 @ 13:45
    Jan Skovgaard
    0

    Hi Andreas

    What version of Umbraco are you using? Since Umbraco 4.5 the XML schema changes and in the above you're looping through each "node" element in the XML. But if you're using a newer version of Umbraco that element does not exist anymore (unless it's an upgraded version where the useLegacyXML has been set to true in the umbracoSettings.config file). The data element and [@alias] stuff is also a leftover from back then. Since you get something when using "/Image" I'm pretty confident you're matching against the new schema.

    It's also a good idea to see what XML the variable is returning, which you can do by writing a textarea like this:

    <textarea>
       <xsl:copy-of select="$mediaFolder" />
    </textarea>

    Also be aware that when using GetMedia extension setting the second parameter to 'true' is the same as writing anything in plings so 'unicorn' will also return true. The same will 'false'. To make sure you give the extension the proper information the second parameter needs to be either 0,1,true() or false() like this

    umbraco.library:GetMedia($mediaFolderId,true()) (no plings!).

    When we're certain what schema you're using we can have a look at a nicer example of how to list the files and medias within the folder.

    Looking forward to hearing from you.

    /Jan

  • Andreas Kristensen 20 posts 79 karma points
    May 20, 2013 @ 14:04
    Andreas Kristensen
    0

    Hi Jan, sorry about the lack of details. 

    I am running version 4.7.0

    Here is my revised code, set to /Image wich returns the one image in the folder.

       <xsl:template match="DKV_filegal">
           
      <xsl:variable name="mediaFolderId" select="number(mediaFolderId)" />
       <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($mediaFolderId, true())" />   
                    <xsl:for-each select="$mediaFolder/Image">

                            <xsl:value-of select="current()/@nodeName" />
          </xsl:for-each>
          
          
        </xsl:template>  
  • Andreas Kristensen 20 posts 79 karma points
    May 20, 2013 @ 14:24
    Andreas Kristensen
    100

    hm, looks like i've been missing the forrest for the trees.

     <xsl:for-each select="$mediaFolder/*">
            <xsl:value-of select="../@nodeName" />/<xsl:value-of select="current()/@nodeName" />/
            <xsl:value-of select="current()/@umbracoFile" />
            <br />
          </xsl:for-each>

    Simply selecting using the * all selector gives me every file in the folder. 

    Looking at my output, it seems like i also get the folder node itself, since there is a node with only the ../@nodeName value.

    Now i just have to make links to the files.

  • Andreas Kristensen 20 posts 79 karma points
    May 20, 2013 @ 14:53
    Andreas Kristensen
    0

    aaand linking fails spectacularly.

    Following code gives me a link to "#" (although each @id is a propper value eg 1976)

    <xsl:variable name="mediaFolderId" select="number(mediaFolderId)" />
    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($mediaFolderId, true())" />   

          <xsl:for-each select="$mediaFolder/*">
              
          <xsl:if test="string(@id) != ''">
              
                 <href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
           </a>
            <xsl:value-of select="@id"/><br/>
           
          </xsl:if>
         
          </xsl:for-each>
          
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 20, 2013 @ 19:25
    Chriztian Steinmeier
    2

    Hi Andreas,

    That's because the NiceUrl() extension only works on Content - the URL for the image is actually available in the umbracoFile property, e.g.:

    <a href="{umbracoFile}">
        <xsl:value-of select="@nodeName"/>
    </a>
    

    /Chriztian

  • Andreas Kristensen 20 posts 79 karma points
    May 21, 2013 @ 09:33
    Andreas Kristensen
    0

    Thanks Chriztian :)

Please Sign in or register to post replies

Write your reply to:

Draft