Tuesday, June 26, 2007

Using DDWRT in xslt-based web parts

What is DDWRT?
well, its a script that microsoft packaged for it's xslt dataviews, that gives them more xslt power.
I needed to use the ddwrt functions in my content query web part, but I guess that the following approach will work in the search web parts as well.

Why do we need it?
I needed it to format the date I was getting back from the content query. The format I was getting back was ugly to the user (2007-06-27 15:52:00) and I wanted to format it, but I didn't want to write my own function.

So how to use it?
you need to add to your xslt the following namespace where the namespaces are declared (at the top of the xsl file, in the "xsl:stylesheet" tag):
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

Then you can use the ddwrt's functions. For example, to format the date I used the following:
<xsl:value-of disable-output-escaping="no" select="ddwrt:FormatDate(string(@Modified), 3081, 5)" />
Note that in the FormatDate function I used 2 hard coded values - 3081 which is the LCID for Australia (so the date will be formatted to Australian date format) and 5 which specifies the what do I want to display - date, time, date and time ect. I have no idea what values give what, but I do know that 5 gives me the date and the time.

9 comments:

Robert said...

Apparently 5 returns the Date Time in the format DateTime as dd/md/yyyy h:mm. Example: "17/08/2000 16:32". "Macaw" has a good article although it relates to SP 2003, the functions are used in 2007 as well.
-- Robert

Steve said...

See http://msdn.microsoft.com/en-us/library/aa505323.aspx for an explanation of each of the options.

Anonymous said...

Ishai Sagi, thanks very much for your suggestion! I changed the number to display dates the Australian way.

Anatoli

Elias said...

I love you!

Frederik Munster said...

Hi, I tried using the ddwrt function, but I get following error:

Cannot find the script or external object that implements prefix 'http://schemas.microsoft.com/WebParts/v2/DataView/runtime'.

Or Error: Namespace 'http://schemas.microsoft.com/WebParts/v2/DataView/runtime' does not contain any functions.

Somebody has any ideas?

Mano Mangaldas said...

Thanks .. that helped a lot.

Refer to this blog for the 2nd parameter i.e. FormatFlag

http://sharethelearning.blogspot.com/2007/05/formatflag-in-ddwrtformatdate-method.html

Jenny said...

Hi!

Here is a list with the formats the different numbers will return:

1 - 11/25/2009
2 - 11/25/2009 11:57 AM
3 - Wednesday, November 25, 2009
4 - 12:57 PM
5 - 11/25/2009 12:57 PM

Harish Mathanan said...

An extremely useful post, glad I found it at just the right time. Thanks again Ishai

Steve.O said...

I put together a small write up on how to integrate this into a Content Query Web Part that might be useful to some. XSL Date formatting in a CQWP. Thanks for the post!