Copied to clipboard

Flag this post as spam?

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


  • Asif Malik 203 posts 339 karma points
    Aug 24, 2010 @ 19:36
    Asif Malik
    0

    XSLT to iterate through the media section

    Hi. I have create a custom media type called Language folder, and essentially copied all properties of a standard folder, and then added a drop down which has entries to determine what contents should be placed within. (this language drop down is a ultimate picker as language is sued elsewhere on the site)

    I would like to create a page which can display all media within a specific folder (ie language) the language code will be supplied by querystring. I have no idea how to go about iterating through all the contents of the Media section (folder), please any help is greatly appreciated.

    Thanks

  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 24, 2010 @ 20:30
    Kim Andersen
    1

    Hi there Asif

    Let's say you have a page with a normal media picker as one of the properties with an alias of "selectedFolder". In this media picker you will pick the folder where you want to show the content from.

    If you want to iterate through all of the, let's say, images in the selected folder, you can do it with an XSLT-file and something like this:

    <xsl:for-each select="umbraco.library:GetMedia($currentPage/selectedFolder, 'false')/Image">
    <xsl:value-of select="@nodeName" />
    </xsl:for-each>

    The above code takes the selected id from the media picker, and iterates through all of the "Images" in that folder (actually the above code only looks at the items that are children of the folder, but if you have more levels those can easily be shown also).

    I hope the code can help you in some way, otherwise just ask again :)

    /Kim A

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 24, 2010 @ 22:14
    Chriztian Steinmeier
    3

    Hi Kim (+ Asif)

    Just because someone looking at the above code (having read about the GetMedia function) would think that the snippet you supplied shouldn't fetch more than a single Image (you're sending false as the booleanDeep parameter), I'll tell you why this actually works:

    'false' is actually going to be interpreted as true by C# because the function expects a boolean value, so the XSLT processor will convert the string to that, using boolean('false')... and that's always going to be the boolean true value, for any string you send (except for the empty string).

    To send the correct boolean 'false' value, you can use either false() or 0 (zero); to send the boolean 'true' value, use true() or 1. 

    So the call to make in the example would be either of these:

    <!-- Using true() -->
    <xsl:for-each select="umbraco.library:GetMedia($currentPage/selectedFolder, true())/Image">
        <xsl:value-of select="@nodeName" />
    </xsl:for-each>
    
    <!-- Using 1 -->
    <xsl:for-each select="umbraco.library:GetMedia($currentPage/selectedFolder, 1)/Image">
        <xsl:value-of select="@nodeName" />
    </xsl:for-each>
    

    Agreed, it's a bit confusing (and I haven't even mentioned what happens if you use true or false without quotes :-)

    Sorry to be stepping on your otherwise perfect example Kim, hope you're okay with it, otherwise I apologize, and owe you a beer :-)

    /Chriztian 

  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 24, 2010 @ 22:35
    Kim Andersen
    0

    Noooo, I made an embarrassing "typo" in my code I can see :D

    I actually tried to create the situation on my local Umbraco, before posting the answer, just to make sure I didn't say something that wouldn't work. I got it working just like I thought it would (with true instead of false - even though it would be the same result I can see with your explanation Chriztian :) ), but just before posting i tried changing the 'true' to 'false' in the code. I must have forgotten to press the ctrl+z before posting my snippet in here.

    But hey, I'm actually glad I posted the "wrong" example, otherwise you probably wouldn't have posted your nice explanation Chriztian, didn't know that 'false' would be interpreted as true in the C#, but now I can for sure understand why :D

    /Kim A

  • Asif Malik 203 posts 339 karma points
    Aug 25, 2010 @ 10:45
    Asif Malik
    0

    Hi Kim/Chriztian, thank you both yoru your detailed response and example code. I was wondering if there is a way to get this workling without having to use a picker.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 26, 2010 @ 21:36
    Kim Andersen
    1

    Without a picker? If you prefer, you can always hardcode an id of a media-folder into the for-each like this:

    <xsl:for-each select="umbraco.library:GetMedia(1212, 1)/Image">
       
    <xsl:value-of select="@nodeName" />
    </xsl:for-each>

    The 1212 is just an example, you will of course put the right id here instead. Was that what you where looking for?

    /Kim A

  • Asif Malik 203 posts 339 karma points
    Aug 26, 2010 @ 22:29
    Asif Malik
    0

    Thats what i figured. I was hoping there was a more elegant way of determining the root media folder. Anyway have gone down the picker route. Thanks for your help guys.

  • Daniel Souchon 2 posts 22 karma points
    Sep 25, 2010 @ 23:48
    Daniel Souchon
    0

    Hi Guys

    This is really what I need and have been trawling the forums like a diligent detective to find something that gets the images from my mediapicker folder so I can display them all on the page.

    When I add the above corrected version by Chriztian to the XSLT I get the following error:

    Why?

     

     

    Error occured

    System.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)
    at System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
    at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

  • Kim Andersen 1447 posts 2196 karma points MVP
    Sep 26, 2010 @ 10:18
    Kim Andersen
    0

    Daniel,

    try creating an if-statement around the for-each. This will make sure that there has been selected a media item in the picker. Like this:

    <xsl:if test="$currentPage/selectedFolder != ''">
    <xsl:for-each
    select="umbraco.library:GetMedia($currentPage/selectedFolder, 1)/Image">
       
    <xsl:value-of select="@nodeName" />
    </xsl:for-each>
    </xsl:if>

    /Kim A

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 26, 2010 @ 10:47
    Dirk De Grave
    2

    In addition to Kim's post, which is exactly the solution, here's some background info why you're getting that error and it's quite simple: as soon as you hit save, the xslt gets parsed (and also run against the top level node) and also trying to execute the GetMedia() function, but $currenPage/selectedFolder may not have a value (until you actually use the xslt on one of your pages that has such a property and a value assigned), whilst GetMedia is expecting a first parameter of type int, so that's why you get the OverflowException.

     

    Hope this helps.

    Regards,

    /Dirk

     

     

Please Sign in or register to post replies

Write your reply to:

Draft