Copied to clipboard

Flag this post as spam?

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


  • Tony Lorentzen 85 posts 174 karma points
    May 29, 2018 @ 21:03
    Tony Lorentzen
    0

    Working with Umbraco UDI in XSLT

    Currently developing a site for a client that needs to be DONE - not perfect ;-) so I'm in dire need to be able to reuse some old XSLT code, but the new Umbraco UDI is causing some issues as you might imagine. What's the best approach to do this? Is there a 'helper' I can use to be able to access the ContentPicker, MediaPicker etc. through XSLT? Thanks guys!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 29, 2018 @ 21:27
    Chriztian Steinmeier
    0

    Hi Tony,

    Do you have a small code snippet that shows what you need? E.g. if you used to just pass the value (a nodeId) into the NiceUrl() extension, is that failing now because it doesn't work with UDIs, or is it maybe returning JSON instead?

    /Chriztian

  • Tony Lorentzen 85 posts 174 karma points
    May 29, 2018 @ 21:35
    Tony Lorentzen
    0

    Usually I'd just get the doc id from something like the ContentPicker so I'd just do a simple:

    And be done with it. Same with a MediaPicker where I'd use the GetMedia helper method because it provided me with the Media ID.

    Now I'm getting umb://media/[GUID] as a value instead. I can convert that to XML but I can't use the GUID for anything to render the URL to my content anyhow.

    Been thinking about making my own XSLT UDI-helper, but if there was an easier fix I'd really appreciate that as I'm kind of in a hurry to get this done :-)

  • Tony Lorentzen 85 posts 174 karma points
    May 29, 2018 @ 21:36
    Tony Lorentzen
    0

    Whops - it ate my code:

    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/myContentPicker)"/>
    

    Simple as that.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 29, 2018 @ 21:51
    Chriztian Steinmeier
    1

    Alright,

    I just remembered I had a similar problem on a site and I can see I wrote my own code to fix the GetMedia() helper:

    /// <summary>
    /// Fix to support getting a Media node's XML even if the identifier is a UDID.
    /// </summary>
    /// <param name="identifier">Either an ID or a UDID as a string</param>
    /// <param name="deep">Boolean flag whether to return children of the node</param>
    public static XPathNodeIterator GetMedia(string identifier, bool deep) {
        Umbraco.Core.Models.IPublishedContent mediaNode;
    
        if (identifier.StartsWith(MediaIdentifier)) {
            mediaNode = umbracoHelper.TypedMedia(Umbraco.Core.Udi.Parse(identifier));
        } else {
            mediaNode = umbracoHelper.TypedMedia(identifier);
        }
        return umbraco.library.GetMedia(mediaNode.Id, deep);
    }
    

    MediaIdentifier is something like this:

    private readonly static string MediaIdentifier = "umb://media/";
    

    I think the NiceUrl() helper actually "just works" with UDIs, as I can see there's no special code for handling that anywhere... but I could be wrong :)

    I'd definitely recommend writing your own if you know how - that way you can also handle null/empty values a lot more graceful than the library method does :-)

    /Chriztian

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Nov 05, 2018 @ 23:02
    Jason Prothero
    0

    Chriztian,

    I'm in the unfortunate position of needed to write this for an older project that I don't have time now to convert everything to Razor.

    Where does the umbracoHelper come from in your example above?

    Boy, I can't find much about XSLT Extensions online anymore...

    Also, how does the Umbraco team not make the XSLT methods work with the new Guid pickers? Help a brother out, huh?

    Thanks, Jason

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 05, 2018 @ 23:52
    Chriztian Steinmeier
    0

    Hi Jason,

    The umbracoHelper was created something like this:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.XPath;
    using System.Web;
    using System.Web.UI;
    using Umbraco.Web;
    using umbraco;
    using umbraco.presentation;
    using HtmlAgilityPack;
    
    namespace ProWorks {
    
        [XsltExtension("proworks.helpers")]
        public class Helpers {
            private readonly static UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
            private readonly static string MediaIdentifier = "umb://media/";
    
            public Helpers() {}
    
            /// <summary>
            /// Fix to support getting a Media node's XML even if the identifier is a UDID.
            /// </summary>
            /// <param name="identifier">Either an ID or a UDID as a string</param>
            /// <param name="deep">Boolean flag whether to return children of the node</param>
            public static XPathNodeIterator GetMedia(string identifier, bool deep) {
                Umbraco.Core.Models.IPublishedContent mediaNode;
    
                if (identifier.StartsWith(MediaIdentifier)) {
                    mediaNode = umbracoHelper.TypedMedia(Umbraco.Core.Udi.Parse(identifier));
                } else {
                    mediaNode = umbracoHelper.TypedMedia(identifier);
                }
                return umbraco.library.GetMedia(mediaNode.Id, deep);
            }
        }
    }
    

    To use it in XSLT, you'd need to declare the namespace with xmlns:helpers="urn:proworks.helpers" (on the root xsl:stylesheet element)

    Hope that helps,

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft