Copied to clipboard

Flag this post as spam?

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


  • Jason Mackay 98 posts 149 karma points
    Apr 30, 2012 @ 12:25
    Jason Mackay
    0

    List all media files, umbraco 4.7

    Hi all,

    I am looking at creating an XSLT/Macro control to display all the media within the media folder(inc PDF's, images, video etc) I have seen a few post around and tried some of them and can't seem to get anything to work, not sure if the properties have changed from the posts I am looking at or my lack of XSLT skills, but does anyone have something I can start with. I understand that a media picker would need to be used also.

    Any ideas?

    Thanks

    Jason

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 30, 2012 @ 12:37
    Chriztian Steinmeier
    1

    Hi Jason,

    This should get you started - you just need to add a mediaFolder parameter on the macro of type "mediaCurrent"...

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:variable name="mediaFolder" select="/macro/mediaFolder" />
    
        <xsl:template match="/">
            <xsl:if test="normalize-space($mediaFolder)">
                <xsl:variable name="media" select="umb:GetMedia($mediaFolder/Folder/@id, true())" />
    
                <!-- If no errors, this will run the Folder template below -->
                <xsl:apply-templates select="$media[not(error)]" />
    
            </xsl:if>
        </xsl:template>
    
        <!-- Template for the folder -->
        <xsl:template match="Folder">
            <ul>
                <!-- Run templates for individual items (just add Media type aliases) -->
                <xsl:apply-templates select="File | Image" />
            </ul>
        </xsl:template>
    
        <!-- Template for items - you can create individual templates to render them differently -->
        <xsl:template match="File | Image">
            <li>
                <a href="{umbracoFile}">
                    <xsl:value-of select="@nodeName" />
                </a>
            </li>
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Jason Mackay 98 posts 149 karma points
    Apr 30, 2012 @ 13:14
    Jason Mackay
    0

    Hi Chriztian

    Thanks very much for your very quick response. I cant seem to get this working, the if statement <xsl:iftest="normalize-space($mediaFolder)"> I'm not sure what it is meant to do, but it doesn't go in to this, which then doesn't go in to the Folder template. Is there something I am meant to do here?
    I created the MediaFolder parameter:

     

    Thanks again for your help.

    Jason

     

     

     

     

     

  • Jason Mackay 98 posts 149 karma points
    Apr 30, 2012 @ 13:22
    Jason Mackay
    0

    Chriztian, if I take out the normalize space I get it to work, seems that it somehow didnt like that. Thats a great starting point for me.

    Thank you again for your quick response.

    Jason

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 30, 2012 @ 13:23
    Chriztian Steinmeier
    0

    Hi Jason,

    That test makes sure that there's something selected in the parameter before trying to call the GetMedia() extension (otherwise we'll get a nasty .NET exception).

    On the template where you insert the macro - were you asked to select a Media folder? You should get something like this after adding the Macro to the template:

    <umbraco:Macro alias="MACROALIAS_HERE" mediaFolder="1212" runat="server" />

     

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 30, 2012 @ 13:28
    Chriztian Steinmeier
    0

    Oh blimey - I keep forgetting this - it's because the mediaCurrent only gives you the empty <Folder> XML - so the normalize-space() will not grab anything.

    If you control everything you can leave it off, but otherwise you can change it to this, to make sure you're not trying to run the extension if no folder has been selected:

    <xsl:if test="$mediaFolder[Folder]">

    Which means "If the mediaFolder variable has a Folder element in it" ...

    /Chriztian

  • Ben Schlaepfer 74 posts 101 karma points
    Jul 18, 2014 @ 12:36
    Ben Schlaepfer
    0

    Hi there,

    This is exactly what I needed to display a list of PDF files in a sub folder of the media library on my Umbraco site. Thanks!

    My XSLT knowledge is pretty scant, but I'd like to be able to sort the results of the macro by the media item title.

    Attempts to use xsl:sort (which I have used with xsl:for-each in the past) within the template block fails.

    Any pointers to ordering the list of media items by the Name property would be highly appreciated.

    Best wishes
    Ben 

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 18, 2014 @ 12:52
    Dennis Aaen
    0

    Hi Ben

    I assume that you are using the code from Chriztian's post. If not could you please copy your code in here, it's easier to help then.

    But if you are using Chriztian's code, I think that you should be able to do something like that.

    <xsl:apply-templates select="$media[not(error)]" />
         <xsl:sort select="@nodeName" />
    </xsl:apply-templates>
    

    Hope this helps,

    /Dennis

  • Ben Schlaepfer 74 posts 101 karma points
    Jul 18, 2014 @ 13:06
    Ben Schlaepfer
    0

    Thanks for the swift reply Dennis.

    Yes, the code is Chriztian's and works great!

    When I add the code you kindly supplied I get an error message when saving the XSLT:

    Error occured
    
    System.Xml.Xsl.XslLoadException: 'xsl:sort' cannot be a child of the 'xsl:if' element. An error occurred at D:\home\site\wwwroot\xslt\635412779284722600_temp.xslt(21,5). 
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 18, 2014 @ 13:19
    Dennis Aaen
    0

    Hi Ben,

    Okay,

    What if you are do the sort in here instead:

    <!-- Template for the folder -->
    <xsl:template match="Folder">
        <ul>
            <!-- Run templates for individual items (just add Media type aliases) -->
            <xsl:apply-templates select="File | Image">
                <xsl:sort select="@nodeName" />
            </xsl:apply-templates">
        </ul>
    </xsl:template>
    

    Hope this helps,

    /Dennis

  • Ben Schlaepfer 74 posts 101 karma points
    Jul 18, 2014 @ 13:22
    Ben Schlaepfer
    0

    Hi Dennis,

    That is where I started! But unfortunately the sort there creates a similar error message:

    Error occured

    System.Xml.Xsl.XslLoadException: 'xsl:sort' cannot be a child of the 'ul' element. An error occurred at D:\home\site\wwwroot\xslt\635412793527439859_temp.xslt(33,9). 
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

     

    Thanks

    Ben

  • Ben Schlaepfer 74 posts 101 karma points
    Jul 18, 2014 @ 13:28
    Ben Schlaepfer
    0

    Hi Dennis,

    That is where I started! But unfortunately the sort there creates a similar error message:

    Error occured

    System.Xml.Xsl.XslLoadException: 'xsl:sort' cannot be a child of the 'ul' element. An error occurred at D:\home\site\wwwroot\xslt\635412793527439859_temp.xslt(33,9). 
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

     

    Thanks

    Ben

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 18, 2014 @ 13:32
    Dennis Aaen
    0

    Hi Ben,

    Okay, have you tried to do the sort inside the li like this:

    <xsl:template match="File | Image">
        <li>
            <xsl:sort select="@nodeName" />
            <a href="{umbracoFile}">
                <xsl:value-of select="@nodeName" />
            </a>
        </li>
    </xsl:template>
    

    Hope this works :-)

    /Dennis

  • Ben Schlaepfer 74 posts 101 karma points
    Jul 21, 2014 @ 15:07
    Ben Schlaepfer
    0

    Hi Dennis,

    Yes I tried in there too.

    The error message was different but still a show-stopper when saving the XSLT.

    System.Xml.Xsl.XslLoadException: 'xsl:sort' cannot be a child of the 'li' element. An error occurred at D:\home\site\wwwroot\xslt\635415447778488841_temp.xslt(44,5).
    

    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

    Below is the code I am trying to add a sort function to, it works well without the XSLT sort code.

    <?xml version="1.0" encoding="utf-8" ?>
    

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:param name="currentPage" />
    
    <xsl:variable name="mediaFolder" select="/macro/mediaFolder" />
    
    <xsl:template match="/">
        <xsl:if test="$mediaFolder[Folder]">
            <xsl:variable name="media" select="umb:GetMedia($mediaFolder/Folder/@id, true())" />
    
            <!-- If no errors, this will run the Folder template below -->
            <xsl:apply-templates select="$media[not(error)]" />
    
    
    
        </xsl:if>
    </xsl:template>
    
    <!-- Template for the folder -->
    <xsl:template match="Folder">
    
        <ul>
            <!-- Run templates for individual items (just add Media type aliases) -->
            <xsl:apply-templates select="File | Image" />
    
    
        </ul>
    </xsl:template>
    
    <!-- Template for items - you can create individual templates to render them differently -->
    <xsl:template match="File | Image">
    
    
    
        <li>
    
            <a href="{umbracoFile}" target="_blank">
                <xsl:value-of select="@nodeName" />
    
            </a>
    
        </li>
    </xsl:template>
    

    Any pointers really welcome!

    Many thanks, Ben

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 28, 2014 @ 16:52
    Chriztian Steinmeier
    0

    Hi Ben,

    I can see why the errors you're getting can seem a little confusing at first, but the processor is really just trying to tell you that you've put the sort element in the wrong place...

    You can use it directly inside a for-each or an apply-templates instruction - but usually, when using apply-templates, you're using the empty (i.e. self-closing) version, so you need to open that one to put the sort element inside, e.g. this:

    <ul>
            <!-- Run templates for individual items (just add Media type aliases) -->
            <xsl:apply-templates select="File | Image" />
    </ul> 
    

    ...becomes this:

    <ul>
            <!-- Run templates for individual items (just add Media type aliases) -->
            <xsl:apply-templates select="File | Image">
                <xsl:sort select="@nodeName" />
            </xsl:apply-templates>
    </ul>
    

    Hope that helps! :)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft