Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14 – Miscellaneous: ddwrt Namespace Functions

This entry is part 14 of 21 in the series Unlocking the Mysteries of Data View Web Part XSL Tags

Cross-posted from EndUserSharePoint.com

If you’ve been following this series, you’ve seen me write about the ddwrt namespace functions multiple times.  They are extremely helpful functions that really aren’t documented in any formal way.  A few of them show up in the XPath Expression Builder, but most of them are a mystery unless you find Serge van den Oever’s great article on MSDN. It’s always amazed me that Serge’s article from October 2005 is all there is, but once you have it, you’re in pretty good shape. (As always, a big “thank you” to Serge!)

As with the other articles in the series, I’m not going to try to cover every single thing that you can do with these functions.  I’ll just show you some of the functions which I use the most often and that I feel have the most utility.  Here’s the full list that Serge covers in his article with links to his descriptions:

AutoHyperLink
AutoNewLine
ConnEnclode
Counter
FieldFilterImageUrl
FieldFilterOptions
FieldPrefix
FieldSortImageUrl
FieldSortParameters
FilterLink
FormatDate
FormatDateTime
GenDisplayName
GenFireConnection
GenFireServerEvent
GetFileExtension
GetStringBeforeSeparator
GetVar
IfNew
IsPrivilegedUser
Limit
ListProperty
MapToAll
MapToControl
MapToIcon
NameChanged
PresenceEnabled
SelectOptions
SetVar
ThreadStamp
Today
TodayIso
UrlBaseName
UrlDirName
UrlEncode
URLLookup
UserLookup

NameChanged

Without a doubt, NameChanged is the function I use the most often.  NameChanged lets you test to see if the value in a sorted column has changed. It’s really handy when you want to group or summarize on a column.

Usually, I use it like this.  Let’s say I have my list items sorted by the columns Region and then State.

<xsl:variable name=“NewRegion” select=“ddwrt:NameChanged(string(@Region), 0)”/>
<xsl:variable name=“NewState” select=“ddwrt:NameChanged(string(@State), 1)”/>
<xsl:if test=“string-length($NewRegion) &gt; 0>
…do stuff for a new Region…
</xsl:if>
<xsl:if test=“string-length($NewState) &gt; 0>
…do stuff for a new State…
</xsl:if>

In the ‘…do stuff…’ sections, I might output a total row for the prior value of State or some summary information for the upcoming value.  It might even be something as simple as outputting a horizontal rule (HR) to separate the sections.  This is also where you can implement expand/collapse logic.  You can use the NameChanged function with multiple columns in the same template by using different values for id (the second argument in the function) for each column.

Today

Simple as it is, this function can come in handy.  There are other ways to get at the current date and time, but this is an easy one:

<xsl:variable name=”Now” select=”ddwrt:Today()”/>

IfNew

If you’d like your DVWP to display items with the little (New!) icon that you see in List View Web Parts (LVWPs), the the IfNew function is what you need. One of the nice things about using the IfNew function is that you can pass in any date value that you like; with LVWPs, it’s always based on the Created date.

<xsl:if test=“ddwrt:IfNew(string(@Created))”>
<IMG SRC=“/_layouts/1033/images/new.gif” alt=“New” />
</xsl:if>

Note that you can display anything that you like using this approach; you’re not required to use the  icon; you can do anything you want!

GetFileExtension and MapToIcon

GetFileExtension returns the extension of a file name.  For instance, if a path to a document in a Document Library is /HR/Policies/2010Policies.docx, the extension is ‘docx’.  This can be handy if you’d like to display the usual icon which represents the file type.  Each icon is stored in /_layouts/images and most take the form icEXT.gif, where EXT is the file extension. MapToIcon is another nice function that helps you find the right icon based on what is registered for each file extension on the server.  (For instance, when you install the PDF iFilter, by default the PDF icon doesn’t adhere to the naming conventions; MapToIcon resolves this for you.)

<img alt=““ src=“{concat('/_layouts/images/', ddwrt:MapToIcon('', ddwrt:GetFileExtension(string(@FileRef))))}”/>

UrlBaseName

UrlBaseName returns the basename of a file name.  For instance, if a path to a document in a Document Library is /HR/Policies/2010Policies.docx, the basename is ‘2010Policies’.  This can be useful in building nicer links to documents, simply because it strips off the path information and the file extension.

<a href="{@FileRef}"><xsl:value-of select="ddwrt:UrlBaseName(string(@FileRef))"/></a>

UrlDirName

UrlDirName returns the directory for a file name.  For instance, if a path to a document in a Document Library is /HR/Policies/2010Policies.docx, the directory is ‘/HR/Policies/’.  This can be useful when you’d like to group documents by their location in the Document Library, perhaps by folder.

<xsl:variable select="ddwrt:UrlDirName(string(@FileRef))"/></a>

UrlEncode

UrlEncode converts string values so that they can be safely passed in URLs.  Certain characters (I won’t cover exactly which here) must be encoded to work.

<a href="/Lists/MyList/Editform.aspx?ID={@ID}&amp;Title={ddwrt:UrlEncode(string($NewTitle))}"></a>

Note that in all of these functions, you need to explicitly convert the values to a string.  (This is usually called ‘casting’ into a new format in programming.)  If you don’t want to understand it, you don’t need to, but if you don’t do this explicit conversion your functions will either fail or return nothing.  I have been stuck by this many times; if your ddwrt function isn’t working, make sure you are converting the parameters to strings.

Obviously, there are quite a few more of these ddwrt functions available to use in your XPath expressions.  I just wanted to show you a few of the little tricks that you can do with them that turn out to be useful over and over again.

In the next article, I’ll go into some more of the built-in functions you can use in your XSL.

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 13 – Miscellaneous: String FunctionsUnlocking the Mysteries of Data View Web Part XSL Tags – Part 15 – Miscellaneous: Field / Node Functions >>

Similar Posts

11 Comments

  1. I am on SP2007. I am trying to create a DVWP that will mimic the calculated column I created, but it doesn’t appear an “IF” statement is allowed in the XPath expression builder. Here is the calculated column:
    =IF(AND(Category=”CR”,[Time Frame]=”One Time”),[Convert 1 TIME],[Estimated Savings])

    [Convert 1 TIME] is also a calculated column. It’s a much simpler calc: [Estimated Savings]/52

    Can I summarize in a DVWP what I am describing above?

    1. Mike:

      XSL syntax is very different than the IF syntax you’re showing here, which mirros Excel logic. You’ll nmeed something *like*:

      <xsl:choose>
        <xsl:when test="@Category = 'CR' and @Time_x0020_Frame = 'One Time'">
          <xsl:value-of select="@Estimated_x0020_Savings div 52"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@Estimated_x0020_Savings"/>
        </xsl:otherwise>
      </xsl:choose>
      

      M.

  2. Hi, Marc … Love the Blog. I’m discovering loads of better ways of doing stuff in SP2007. With ref to the NameChanged function, I’m trying to display values from a list grouped together under headings (the same way you use New Group in a Summary Link WP). Before NameChanged came along, I’d just put an If condition in that says * xsl:if test=”position() = 1″ * and the first item in the list would render with a heading. But now the NameChanged function makes it possible to have multiple headings, I’m struggling to find a way to present the heading at the beginning of each section.

    1. Alan:

      You can still do the same thing for position()=1, which would be the first item.

      The “do stuff” sections in this post is where you would put your code to emit changes when each of those values changes.

      M.

  3. I am displaying Images in dataview webpart from picture library and want images to be linked to other page when I clicked on that image and also if I want to wrap the metadata of image to link to the image.
    so please show me how should I do that?

    1. kriss:

      You can emit any markup you choose in the XSL. Just decide what you want it up be, add that markup where the image is, and replace the hard values with the values from the items.

      M.

  4. Marc,

    Thanks a TON for solving the XSLT puzzle. The best help on the topic so far.

    Need a help !

    I have 3 inter connected XSLT webparts (Parent1, Parent2, Child1). I have modified the ‘add new item’ of the Parent2 webpart so that the &Parent1ID=123 is passed in the URL to a InfoPath which then fetches some related data on the form. it works fine.

    For the Child1, i need the ID of the Parent2 – I am able to get this with the parameters passed between them and works fine. Applied filter on those parameters on child webparts and filters fine.

    The challenge is after selecting a Parent2 record (parameter is passed to Child1), user goes back and selects a different Parent1 record (Parent 2 is refreshed automatically), but the parameter passed earlier by Parent2 to Child1 stays – as user has not yet selected any Parent2 record at this time after the change. In this state, if the user press ‘add new’ in the Child1, it will add record for the previously selected Parent2.

    How do I ::

    (a) Find out that “select” icon is selected (the double side arrow icon that appears in any parent record in XLST) in the connected webpart Parent2 and get that value (true/false) to Child1 XLST.

    (b) Disable the ‘add new’ link in the Child1 XSLT when no Parent2 record is selected (jquery/javascript?)

    Thanks much in advance.

    Santosh Balan

  5. Very well done and thank you for the x-links on information. Hope you keep this post alive for as long as SP2013 is alive.

    Could you help me find information on “http://schemas.microsoft.com/WebParts/v3/rssagg/runtime”?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.