Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 11:19
    Kasper Dyrvig
    0

    Why doesn't GetMedia work?!

    I'm tring to get a picture out via MediaPicker and XSLT... But why does it not work?!

    The XML-document is taken from the querystring. Then I get data by <xsl:value-of select="datafield" />. That works perfect.

    But when I get to the media-part I get "0" as output. My code is: <xsl:value-of select="umbraco.library:GetMedia(@alias = 'image', 'false')/data [@alias = 'umbracoFile']"/>

    Another problem is to get the actual image displayed...

    Can someone help me? 

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 11:28
    Lee Kelleher
    0

    Hello ,

    Which version of Umbraco are you using?  If you are using 4.0.x, then I wrote a blog post about how to use GetMedia:

    http://blog.leekelleher.com/2009/11/30/how-to-use-umbraco-library-getmedia-in-xslt/

    If you are using v4.5 - my blog post will still be helpful, but the XML schema is slightly different.

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 12:01
    Kasper Dyrvig
    0

    I'm using 4.5.

    I'll read your blog now...

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 12:07
    Lee Kelleher
    0

    Hi Webspas,

    OK, for v4.5 - the new XML schema is to swap out the "node" with the nodeTypeAlias and the "data[@alias]" with the property alias name.

    Here's an example - based on the code snippet from my blog post:

    <xsl:template match="/">
        <xsl:variable name="mediaId" select="number($currentPage/mediaId)" />
        <xsl:if test="$mediaId &gt; 0">
            <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
            <xsl:if test="count($mediaNode/data) &gt; 0">
                <xsl:if test="string($mediaNode/umbracoFile) != ''">
                    <img src="{$mediaNode/umbracoFile}" alt="[image]">
                        <xsl:if test="string($mediaNode/umbracoHeight) != ''">
                            <xsl:attribute name="height">
                                <xsl:value-of select="$mediaNode/umbracoHeight" />
                            </xsl:attribute>
                        </xsl:if>
                        <xsl:if test="string($mediaNode/umbracoWidth) != ''">
                            <xsl:attribute name="width">
                                <xsl:value-of select="$mediaNode/umbracoWidth" />
                            </xsl:attribute>
                        </xsl:if>
                    </img>
                </xsl:if>
            </xsl:if>
        </xsl:if>
    </xsl:template>

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 12:23
    Kasper Dyrvig
    0

    There is one problem with this... for some reason I can't use $currentPage. I not exacly sure why, but I think it has something to do with the fact that I'm desciding witch xml-document to view from the querystring...

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 12:26
    Lee Kelleher
    0

    Do you have the "currentPage" param in your XSLT?

    <xsl:param name="currentPage" />

    If not, then add it - just above/outside the first <xsl:template>.

    Not sure what you mean about selecting different XML documents from the querystring.  Are you using the XSLT via a macro? If so, then you'll still have access to "currentPage".

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 12:41
    Kasper Dyrvig
    0

    Yes I have the currentPage, but I get an error every time I want to use it.

    Here is the essentials of the code:

    <xsl:for-each select="umbraco.library:GetXmlNodeById(umbraco.library:RequestQueryString('Id'))"><xsl:value-of select="omrdenavn" /><xsl:value-of select="umbraco.library:GetMedia(@alias = 'graf', 'false')/data [@alias = 'umbracoFile']"/></xsl:for-each>


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 12:49
    Lee Kelleher
    0

    Hi Webspas,

    Try this XSLT... I have added comments to explain further...

    <!-- gets the ID from the QueryString -->
    <xsl:variable name="id" select="number(umbraco.library:RequestQueryString('Id'))" />
    
    <!-- gets the XML node for the ID -->
    <xsl:variable name="node" select="umbraco.library:GetXmlNodeById($id)" />
    
    <!-- not sure what you are trying to loop through here ... if its child nodes, then use "$node/*[@id]" instead -->
    <xsl:for-each select="$node">
        <!-- call GetMedia using the 'graf' property alias -->
        <xsl:value-of select="umbraco.library:GetMedia(graf, 0)/umbracoFile" />
    </xsl:for-each>
    
    I'd want to add a few more checks, to make sure that the $id contains a value... and then that the GetXmlNodeById actually returns a nodeset before I try to loop through it.  Again, depending what you want to loop through - guessing its child/sub-nodes?

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 12:55
    Kasper Dyrvig
    0

    Ah... I can see the cleverness in this. 

    What if... is it possible to do sime kind of "GetXmlNodeByName" insted of Id?

    I really appreciate your assistance!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 13:00
    Lee Kelleher
    0

    Hi Webspas,

    There is a function called GetXmlNodeByXPath - which you could use to do something like this:

    <xsl:variable name="name" select="umbraco.library:RequestQueryString('Name')" />
    <xsl:variable name="node" select="umbraco.library:GetXmlNodeByXPath(concat('//*[@nodeName=&quot;]', $name, '&quot;]'))" />

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 13:25
    Kasper Dyrvig
    0

    I must be really bad at this, because I just can't get this working! :-(

    If I write this

    <xsl:value-of select="umbraco.library:GetMedia(@alias = billede, 0)/data [@alias = 'umbracoFile']"/>

    I get "No media is maching '0'" as output.

    If I do this

     <xsl:value-of select="umbraco.library:GetMedia($currentPage/data[@alias='billede'], 0)/data [@alias = 'umbracoFile']"/>
    
    
    

    I get error: "System.OverflowException: Value was either too large or too small for an Int32."

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 13:28
    Lee Kelleher
    0

    Remove the @alias bit, you don't need it in v4.5 (thats for the old XML schema).

    <xsl:value-of select="umbraco.library:GetMedia(billede, 0)/umbracoFile"/>
    

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 15:28
    Kasper Dyrvig
    0

    I'm very sorry... but I still get an error..."Value was either too large or too small for an Int32."

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 15:30
    Lee Kelleher
    0

    Is this in the XSLT editor (in the Umbraco back-office)?

    Try this...

    <xsl:if test="string(billede) != ''">
        <xsl:value-of select="umbraco.library:GetMedia(number(billede), 0)/umbracoFile"/>
    </xsl:if>

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 15:31
    Kasper Dyrvig
    0

    Okay. Now I don't get the error, but there is still no image.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 15:35
    Lee Kelleher
    0

    Its pretty much impossible for me to help without seeing any XML data.

    Use the following to output the XML and copy-n-paste it on here.

    <xmp><xsl:copy-of select="umbraco.library:GetMedia(billede, 0)"/></xmp>

    If the XSLT editor shows an error, just click the ignore error checkbox.

    - Lee

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 15:46
    Kasper Dyrvig
    0

    Got it:

     

    <Image id="1145" version="1accfbfa-ee23-434b-92bf-42efe05d1367" parentID="1117" level="2" writerID="2" nodeType="1032" template="0" sortOrder="2" createDate="2010-07-28T10:31:53" updateDate="2010-07-28T10:31:53" nodeName="Family Ski" urlName="familyski" nodeTypeAlias="Image" path="1,1117,1145"><umbracoFile>/media/452/familieski.jpg</umbracoFile><umbracoWidth>591</umbracoWidth><umbracoHeight>304</umbracoHeight><umbracoBytes>178274</umbracoBytes><umbracoExtension>jpg</umbracoExtension></Image>

     

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 15:50
    Lee Kelleher
    0

    Ah, I forgot, you're using v4.5 ... any chance you are going to upgrade to v.4.5.1?  There was a minor bug with GetMedia in v4.5.0.

    If you don't want to upgrade - you'd be crazy not to! But use the following XSLT:

    <xsl:value-of select="umbraco.library:GetMedia(number(billede), 0)/Image/umbracoFile"/>

    v4.5. needs to have the /Image part in the XPath.  You don't need that bit in v4.5.1.

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 28, 2010 @ 15:59
    Kasper Dyrvig
    0

    Yeaha! I can see the image!

    Thank you very much Lee! I would never have found out that there was a bug...

    Well now I only have to find out a few other things. But I won't disturb you more.

    Thank you again!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 28, 2010 @ 16:01
    Lee Kelleher
    0

    No problem, glad that GetMedia is working for you now! :-D

    Cheers, Lee.

    PS. Don't forget to mark the solution (for others who have the same issue).

Please Sign in or register to post replies

Write your reply to:

Draft