Accessing media files

To access media from the media section use the umbraco.library:GetMedia library function.

In a macro with a mediaCurrent (media picker) parameter called MediaNode we could use the following code to access the picked media:

<xsl:variable name="MediaNodeId" select="/macro/MediaNode/node/@id"/>
...
<xsl:if test="$MediaNodeId != '' ">
...
  <!-- Show url -->
  <xsl:value-of select="umbraco.library:GetMedia($MediaNodeId, 'false')/data [@alias = 'umbracoFile']"/>
  ...
  <!-- Show image -->
  <img>
    <xsl:attribute name="src">
      <xsl:value-of select="umbraco.library:GetMedia($MediaNodeId, 'false')/data [@alias = 'umbracoFile']"/>
      </xsl:attribute>
  </img>
...
</xsl:if>

See the Accessing Node Information hint-page for a more details on media node attributes and properties.

One important thing to note in when accessing the media library using GetMedia, the xslt will fail to save without errors due to validation of the nodeId parameter to GetMedia. To get around this issue you need to make sure the nodeId is not empty - thus the <xsl:if test="$MediaNodeId != '' "> check is added.

Displaying all media under a picked media node (in this example specified in a MediaPicker type argument $MediaNode on the macro):


<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:umbraco.library="urn:umbraco.library" exclude-result-prefixes="msxml umbraco.library"> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:param name="currentPage"/> <xsl:variable name="MediaNodeId" select="/macro/MediaNode/node/@id"/> <xsl:template match="/"> <table> <xsl:if test="$MediaNodeId != '' "> <!-- just added as cast-workaround --> <xsl:for-each select="umbraco.library:GetMedia($MediaNodeId, 'false')/node"> <tr> <td> <h5 class="tight"> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:if test="@nodeTypeAlias='File'"> <xsl:value-of select="data [@alias = 'umbracoFile']"/> </xsl:if> <xsl:if test="@nodeTypeAlias='Image'"> <xsl:value-of select="data [@alias = 'umbracoFile']"/> </xsl:if> <xsl:if test="@nodeTypeAlias='Link'"> <xsl:value-of select="data [@alias = 'dokUrl']"/> </xsl:if> <xsl:if test="@nodeTypeAlias='Folder'"> ?MediaRoot=<xsl:value-of select="@id"/> </xsl:if> </xsl:attribute> <xsl:value-of select="@nodeName"/> </xsl:element> </h5> </td> <!-- Add other <td> entries here to display further info --> </tr> </xsl:for-each> </xsl:if> </table> </xsl:template> </xsl:stylesheet>

 


Comments:

1. februar 2010:

Hello, Nice code! Just wondering how i state which media folder i want to start from? I have a folder that i need users to view the contents of, this folder is called 'Documents', it has sub-folders and umbraco recognises this folder as: http://www.server.com.au/media/39780/ how do i make this the start folder? Thanks! Nick McComb.

Commented by: Nicholas McComb

1. februar 2010:

Hi Nick, the folder containing all medias to list is specified by a "MediaNode" macro argument, so you have to create a MediaPicker argument called "MediaNode" on your macro. When inserting the macro in e.g. a template you select the folder to use. If you want to list subfolders recursively you have to modify the script. Currently it only lists the content of selected folder - this is specified in for-each clause. If you need to list the content of subfolders you may have to add another level of looping, or maybe use recursion in the script.

Commented by: Tommy

2. februar 2010:

I have the macro in my template now. I'm just wondering what i put as the MediaNode string for the folder path? My folder location is called 'MediaDocFolder' and is here: Media - FolderOne - FolderTwo - MediaDocFolder Item1.jpg Item2.jpg Item3.doc

Commented by: Nicholas McComb

2. februar 2010:

There is the media folder, then inside that is FolderOne, then inside that is FolderTwo, then inside that is the MediaDocFolder with some images and a document. I have tried "/media/FolderOne/FolderTwo/MediaDocFolder/" as the string for the property in the template but this didn't work :(

Commented by: Nicholas McComb

2. februar 2010:

Hi Nick. The MediaNode property is not a folder string - it is the result of a MediaPicker property type, i.e. it is the node of the media folder you pick when inserting the macro in your template using the "Insert Macro" button in the template editor. What is actually inserted into your macro call is the node is of the media node you pick. You are welcome to send me your script and your macro call parms, and then I can review it. Send it to tommy at blackpoint.dk

Commented by: Tommy

2. februar 2010:

Excellent! I got it :) The problem was that i was just typing in the macro instead of inserting it through the tool bar! Silly me.. lol Thankyou for all your help!!! You are awesome!!!

Commented by: Nicholas McComb

4. februar 2010:

I'm working on making this recursive for sub folders at the moment, i'm not great with XSLT or Umbraco so if i get it to work ill send it to you

Commented by: Nicholas McComb