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.
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:
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 :)
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:
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
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.
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.
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)
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.
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
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:
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
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:
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
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
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.
Without a picker? If you prefer, you can always hardcode an id of a media-folder into the for-each like this:
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
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.
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)
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:
/Kim A
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
is working on a reply...