I'm just starting to play with the XSLT system in umbraco where I was hoping to produce a macro which listed all the media under a specific media directory. I have come across umbraco.library:GetMedia but, frankly, I have no idea what to pass to it in order to get a list of items. The API docs at http://umbraco.org/apiDocs/html/M_umbraco_library_GetMedia.htm seem to suggest that what I probably want is to look up a node (how?) and then pass it in with
umbraco.library:GetMedia(<some node id>, true)
How would I go about getting that initial node id?
That was tantalizingly close to what I wanted. The only issue is that the media name you gave actually outputs the name of the directory over which I'm running my script. I managed to get it in the end withjust
This script can also handle nested directories. I'm ignoring them at the moment but others might wish to recurse into them such that we could get an entire tree structure. This feels a lot like something to develop into a package.
Retrieving a list of media
Hi,
I'm just starting to play with the XSLT system in umbraco where I was hoping to produce a macro which listed all the media under a specific media directory. I have come across umbraco.library:GetMedia but, frankly, I have no idea what to pass to it in order to get a list of items. The API docs at http://umbraco.org/apiDocs/html/M_umbraco_library_GetMedia.htm seem to suggest that what I probably want is to look up a node (how?) and then pass it in with
umbraco.library:GetMedia(<some node id>, true)
How would I go about getting that initial node id?
Subsiquently would something like this work?
<xsl:for-each select="umbraco.library:GetMedia(<SOMEMAGIC>, 'true')">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
</li>
</xsl:for-each>
Either a hard coded id (the id of the media folder) or a generic property via a media picker.
Add a media picker to the document type you want where the user can select the media folder to iterate through.
hth, Thomas
That mostly worked. I retrieved the nodes based on a media picker output, however what I get out seems to be a bit nonsensical. I did this:
<xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
name is: <xsl:value-of select="."/>
<li>
<xsl:for-each select="@*">
Attribute name=<xsl:value-of select="name()"/><br/>
Attribute value=<xsl:value-of select="."/><br/>
</xsl:for-each>
</li>
</xsl:for-each>
The inner loop is just querying the node attributes for me to see if they are useful (they aren't). The <value-of select="."/> returns something like
/media/303/hydrangeas.jpg1024768595284jpg
where the actual file name is
/media/303/hydrangeas.jpg
What is this extra stuff appended to the end and is it possible to just get the actual path? I can paste full output but it is a bit verbose.
Hi Simon, Welcom to the Umbraco forum!
What you need within your for loop is this.
Media name:
Media location:
Welcome that is....it's an early morning.
That was tantalizingly close to what I wanted. The only issue is that the media name you gave actually outputs the name of the directory over which I'm running my script. I managed to get it in the end withjust
<xsl:when test="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']">
So the completed script looks like
<xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
<li>
<xsl:choose>
<xsl:when test="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']">
<a><xsl:attribute name="href">
<xsl:value-of select="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']"/>
</xsl:attribute>
<xsl:value-of select="@nodeName"/>
</a>
</xsl:when>
<xsl:otherwise>
<!--Do something with the directory-->
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
This script can also handle nested directories. I'm ignoring them at the moment but others might wish to recurse into them such that we could get an entire tree structure. This feels a lot like something to develop into a package.
He he he, this one checks if the media is an image and if so displays the thumbnail. This umbraco thing is awsome!
<xsl:if test="$currentPage/data [@alias='mediaDir']">
<xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias='mediaDir'], 'true')/node">
<xsl:choose>
<xsl:when test="@nodeTypeAlias = 'Image'">
<li>
<a><xsl:attribute name="href">
<xsl:value-of select="umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']"/>
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="Exslt.ExsltStrings:replace(umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile'], '.', '_thumb.')"/>
</xsl:attribute>
</img>
<xsl:value-of select="@nodeName"/>
<xsl:value-of select="@nodeTypeAlias"/>
</a>
</li>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:if>
is working on a reply...