Lee Kelleher’s Weblog

random posts on code, .NET, Umbraco and WordPress

Umbraco: Ultimate Picker XSLT Example

with 8 comments

Chatting with Dan (my partner-in-code at Bodenko) about the Ultimate Picker data-type in Umbraco, we realised that we couldn’t find any examples of how to use the data in XSLT. So obviously needing an excuse to write-up a new blog post, here we go.

If you need a quick overview about the Ultimate Picker data-type, see Tim Geyssens’ blog post.

For my example, using a default Umbraco install (with Runway), we will create a new data-type using the Ultimate Picker, (let’s call it “Runway Textpage Picker“), we select the ‘Database datatype‘ to be “Nvarchar”; the ‘Type‘ as a “List Box”; The ‘Parent nodeid‘ is “1048″ and the ‘Document Alias‘ filter is “RunwayTextpage” – we also tick the ‘Show grandchildren‘ checkbox.

Ultimate Picker Data Type settings

Ultimate Picker Data Type settings

Next, we will assign the new “Runway Textpage Picker“ data-type to a property of the Runway Textpage, we will call it “Related Content“. For more information about working with document types, please refer to the our.umbraco.org wiki page, (Working with document types).

Adding Ultimate Picker to a Document Type

Adding Ultimate Picker to a Document Type

Once the document-type is saved, we move on to the ‘Content‘ section. Select any of the Runway Textpages.  You should now see the ‘Related Content‘ property panel, containing a list of all the other Runway Textpages.  To select multiple items, hold-down the CTRL (or Command on the Mac) button.  When you have finished, click the ’Save and publish‘ button.

Selecting related content

Selecting related content

For the next part we get to the real meat of this blog post… the XSLT!

Create a new XSLT

Create a new XSLT

Create a new XSLT called “RelatedContent” (without the .xslt extension), keep it ‘Clean’ and tick the ‘Create Macro‘ checkbox.  Next a quick short-cut for you; copy-n-paste the following XSLT into the main editor window.

Copy-n-paste the XSLT

Copy-n-paste the XSLT

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#xA0;"> ]>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxml="urn:schemas-microsoft-com:xslt"
	xmlns:umbraco.library="urn:umbraco.library"
	exclude-result-prefixes="msxml umbraco.library">
	<xsl:output method="xml" omit-xml-declaration="yes"/>

	<xsl:param name="currentPage"/>

	<xsl:template match="/">
		<xsl:variable name="preNodes">
					<xsl:variable name="relatedContent" select="$currentPage/data[@alias='RelatedContent']" />
					<xsl:variable name="nodeIds" select="umbraco.library:Split($relatedContent, ',')" />
					<xsl:for-each select="$nodeIds/value">
						<xsl:copy-of select="umbraco.library:GetXmlNodeById(.)"/>
					</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="nodes" select="msxml:node-set($preNodes)/node" />
		<xsl:if test="count($nodes) > 0">
<div class="related-content">
<h3>Related Content</h3>
<ul>
					<xsl:for-each select="$nodes">
	<li>
							<a href="{umbraco.library:NiceUrl(@id)}">
								<xsl:value-of select="@nodeName" />
							</a></li>
</xsl:for-each></ul>
</div>
</xsl:if>
	</xsl:template>

</xsl:stylesheet>

Here’s a quick explanation of what is happening in the XSLT.  We are provided with a list of comma-separated nodeIds from the Ultimate Picker, which we need to parse/split – then pull back the XML node data, then we can transform it however we like!

The way we do this is 2-tiered, first we must loop through the comma-separated list, pulling back the XML node for each nodeId, adding it to an XSLT variable.  Doing this will cause the XSLT variable to be a fragmented node-tree, which means that we need to convert the node-tree fragment into a node-set.  (*Note: there are a gazillion ways to skin a cat – suggestions are welcome – this method works for me).

Once we have the complete XML node-set, we can transform into whatever HTML we  like.

Now that we are done with the XSLT, we can edit our template in the ‘Settings‘ section. Select the “Runway Textpage” template. Somewhere after the ‘bodyText‘ property item, click on the ‘Insert Macro’ button.  From the ‘Choose a macro‘ drop-down, select the ‘Related Content‘ option – this will add the macro code to the template.

Insert the macro into the template

Insert the macro into the template

Save the template and view the front-end page. We can now see the Related Content pages. Tada!

The end result! Related Content

The end result! Related Content

Written by Lee Kelleher

September 8, 2009 at 10:38 pm

8 Responses

Subscribe to comments with RSS.

  1. Great post and excellent detail!

    But no mention of RUM?!

    Chris

    September 9, 2009 at 8:08 am

    • Cheers Chris

      Didn’t I mention that Dan and I were chatting about it over a few glasses of rum? Must have forgot, I blame the rum!

      Lee Kelleher

      September 9, 2009 at 8:44 am

  2. Hi Lee,

    Would be great to have this added to the wiki section, as it has a section that summarizes the default datatypes included in an umbraco install…. which reminds me I should do the same :)

    Cheers,
    Dirk

    Dirk De Grave

    September 10, 2009 at 1:24 pm

  3. Hi,
    I’m using the ultimate picker in my umbraco site.
    The problem I encourred is select nodes filter by the nodes selected with ultimate picker.

    Example:
    With the ultimate picker I select 3 nodes and the result is a string with these 3 nodes comma separated (ex. 100,150,200)
    Now I would to retrieve my nodeList without the nodes listed into ultimate picker fields.
    Can you help me to resolve this issue?

    Sorry My not perfect english

    By
    Adriano

    Adriano Fabri

    October 26, 2009 at 6:55 pm

    • Hi Adriano, thank you for your comment.

      I am trying to understand your problem, do you mean that you want to exclude the selected node IDs?

      Lee Kelleher

      October 26, 2009 at 8:06 pm

      • Hi Lee,
        yes I would to exclude selected node IDs.

        Can you help me???

        Thank you in advance.
        Adriano

        Adriano Fabri

        October 26, 2009 at 9:14 pm

      • Hi Adriano, sorry for not getting back to you… I’ve been working away a lot recently!

        If you haven’t already done so, your best bet is to ask the question on the Our Umbraco Community forums. You’ll be sure to get an answer pretty quick!
        http://our.umbraco.org/forum/developers/xslt

        Good luck and all the best!

        Lee Kelleher

        November 2, 2009 at 6:20 pm

  4. Very nicely explained. Great work!!!!!

    Ravi Gupta

    March 30, 2010 at 1:32 pm


Leave a Reply