Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
Author: Marc D. Anderson
http://mdasblog.wordpress.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:
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 > 0)> …do stuff for a new Region… </xsl:if> <xsl:if test=“string-length($NewState > 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 name="DirName" 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}&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.
Author: Marc D. Anderson
http://mdasblog.wordpress.com
Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA. He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes. Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice. Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
- Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
- Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
- EUSP eBook Store: First SharePoint Title is Now Available
HS!!! I can’t believe this info is free!
Marc,
Thank you again for providing insight into this area. I’ve really been diving into XSLT lately and love what it delivers to the end user. Combined with jQuery, you can really do some nice stuff.
Very interesting, will have much use for this. Have you used/is it possible to use URLLookup to create joined list views or display user profile image, colums from another list etc?
@Matt and @Chriss: Glad you’re liking this info. Are you starting to see why I like the DVWP so much?
@Fredrik: I haven’t used URLLookup that much, frankly. I tend to use AggregateDataSource and do the lookups myself in the XSL instead. If you’re interested in looking up info in the User Information List, you might be interested in this blog post of mine.
M.
I agree that Serge’s article is an invaluable resource.
Mark, is there a way to add other XSLT libraries (like eXSLT) to SharePoint?
@Christophe: I have no idea. What would you be looking to gain?
Well, you’d gain access to the extensions provided by these libraries, the same way you access ddwrt functions. This could include date and time manipulations, regular expressions, random numbers, etc.
See here the eXSLT functions (this is just one example of XSLT library):
http://www.exslt.org/
@Christophe: Interesting. I wasn’t familiar with eXSLT. I just looked at the date/time functions, and they sure woulds be useful. I have to believe that the answer is “yes”. Since the functions look to be implemented in some mixture of JavaScript and XSL, I’m guessing that there might be some installation on the server required.
However, it also looks like many of the functions have a pure XSL template implementation. This is the sort of stuff that I usually build and put into my utilities.xsl file and then import into DVWPs where needed. So you could do the same thing with the eXSLT function templates.
Have you been getting my emails?
Hi Marc.
Thank you very much for this articles.
Do you know if is possible to combine two ddwrt function?
I wish i could combine the “GenFireConnection” function whit the “GenFireServerEvent” function to connect and filter together a datawebpart to another datawebpart.
Can you help me ??
Thanks Lorenzo.
@Lorenzo: If I understand what you’re trying to do, I usually just do a redirect back to the same page with the values on the Query String. Are both DVWPs on the same page?
How would you change the XSL so it will support multivalue lookup fields…?
I basically want group by the values in my mutlivalue lookup and then provide a count i.e.
item 1 has a,b,c
item 2, ab
i would then have the following grouping structure output
Group a
->item1
->item2
Group b
->item1
-> item2
Groub c
->item1
thank You :-)
Sorry for my english if I haven’t explained well the question.
The DVWPs are on the same page.
I want when I click on the highlight fields on the first DVWP (and the first DVWP send a row to the second DVWP by calling the “GenFireConnection” function) the “GenFireServerEvent” function is called to filter the DVWP.
In this way the first DVWP will show me only the row on which I clicked and the second DVWP show me only the rows that have a connection with the first DVWP.
I hope you understand me.
Thanks Lorenzo.
@Mark: I build recursive templates to deal with multi-value columns. Check these blog posts of mine:
http://mdasblog.wordpress.com/2008/12/03/filtering-on-a-value-in-a-multi-select-lookup-column/
http://mdasblog.wordpress.com/2009/06/11/displaying-a-multi-select-column-nicely/
@Lorenzo: I really am not a fan of Web Part connections. I’d set both DVWPs to filter based on a Query String value (with a default if there isn’t one, if needed) and then just pass the value in the links.
Thanks for the query string tip.
Now I have another question. Is it possible to have the DVWP not filter by default when query string filter is created?
If I create a parameter for use with the query string and I impose no value by default the DVWP doesn’t show me any value. When I pass the query string the DVWP shows me only the value that I passed.
Thanks
Lorenzo
@Lorenzo: It’s just a matter of setting your filtering correctly. Something like this:
Marc,
Have you every managed to get the ddwrt:URLLookup function working, it seems to be bugged in WSS3.0 and MOSS2007. Don’t know if you have had any experience with it and if you managed to get it working?
@Adam: Frankly, I’ve never used the ddwrt:URLLookup function (haven’t seen the need) but I’ve had several people ask me about it. Why don’t you post what you’re trying toaccomplish in Stump the Panel, and we’ll see how best to solve it.
M.
Hi Marc,
I am trying to get hold of the current User’s email by using the UserLookup function. The documentation suggests that this should be available by using the ‘Email’ parameter but all I get back is the User’s name. Is there a setting that enables this feature or something I am missing on how to use it.
Regards,
Steve.