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!
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?
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 :-)
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);
}
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 :-)
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)
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!
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
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 :-)
Whops - it ate my code:
Simple as that.
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:MediaIdentifier is something like this:
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
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
Hi Jason,
The
umbracoHelper
was created something like this:To use it in XSLT, you'd need to declare the namespace with
xmlns:helpers="urn:proworks.helpers"
(on the rootxsl:stylesheet
element)Hope that helps,
/Chriztian
is working on a reply...