Thursday, June 4, 2015

XSLT date sorting in RSS viewer Web Part

Recently i encountered a issue while sorting in RSS Viewer web part. In xslt you can sort only on number and text data type and no option for date-time data type.

I wanted to sort data on pubDate of RSS feed. I tried using the SharePoint designer and use the sort and group option but  did not work.
The trick is to convert the pubDate to some format and then split the year, month and day and then sort on these 3 parts.

Example:

In my case pubDate is in this format Wed, 20 May 2015 07:02:09 +0200
I format this using FormatDate(pubDate,number(2057),1) and output is 20/06/2015.
Then split the year 2015, month 06 and day 20 using substring method and then sort these respectively.

You have to use the xsl:sort under RSSMainTemplate.body template after the xsl:for-each.

Source Code:

<xsl:for-each select="$Rows">
<xsl:sort  order="descending" select="substring(ddwrt:FormatDate(pubDate,number(2057),1),7)"></xsl:sort>

  <xsl:sort  order="descending" select="substring(ddwrt:FormatDate(pubDate,number(2057),1),4,2)"></xsl:sort>
  <xsl:sort  order="descending" select="substring(ddwrt:FormatDate(pubDate,number(2057),1),1,2)"></xsl:sort>


Happy Coding !!!

No comments:

Post a Comment