Sharepoint 2010 XSLT Convert Number to Month in order to sort groups

SharePoint lists are great for documents or simple data. Often data contains a special date, timeline, a date for which it is valid or which is important for the data entry. In the listview it might be cool to have a nice view of those entries which belong to a month or year or both. It would be cool to group items by year and month for example.

One of the requirements for a customer project was exactly this. An external list displays items which should be grouped by month.So i created a column in the bdc model for the month which has a value year month (yyyy MM, e.g. 2013 07). It also works if you are using a calculated column, but i had to do it this way cause i am working with an external list. In this case the listview is looking like this:

month1

But instead of month numbers there should be displayed the month name. If i save the month name into the field the ordering goes wrong cause it orders alphabetically (december and then july) but i want the right order in the month. So i decided to save month number and change the listview rendering. In the end it looks as desired:

month

In the xsl of the listview i changed the display behavior.

At first i created a new xsl template (works like a function) and called it “get-month-name”. It has two parameters the month and the year, cause the output will be put together with “year monthname”.



<xsl:template name="get-month-name">
<xsl:param name="month"></xsl:param>
<xsl:param name="year"></xsl:param>

<xsl:choose>
<xsl:when test="$month = 01"><xsl:value -of select="$year"></xsl:value>Januar</xsl:when>
<xsl:when test="$month = 02"><xsl:value -of select="$year"></xsl:value> Februar</xsl:when>
<xsl:when test="$month = 03"><xsl:value -of select="$year"></xsl:value> März</xsl:when>
<xsl:when test="$month = 04"><xsl:value -of select="$year"></xsl:value> April</xsl:when>
<xsl:when test="$month = 05"><xsl:value -of select="$year"></xsl:value> Mai</xsl:when>
<xsl:when test="$month = 06"><xsl:value -of select="$year"></xsl:value> Juni</xsl:when>
<xsl:when test="$month = 07"><xsl:value -of select="$year"></xsl:value> Juli</xsl:when>
<xsl:when test="$month = 08"><xsl:value -of select="$year"></xsl:value> August</xsl:when>
<xsl:when test="$month = 09"><xsl:value -of select="$year"></xsl:value> September</xsl:when>
<xsl:when test="$month = 10"><xsl:value -of select="$year"></xsl:value> Okotber</xsl:when>
<xsl:when test="$month = 11"><xsl:value -of select="$year"></xsl:value> November</xsl:when>
<xsl:when test="$month = 12"><xsl:value -of select="$year"></xsl:value> Dezember</xsl:when>
<xsl:otherwise>error: <xsl:value -of select="$month"></xsl:value></xsl:otherwise>
</xsl:choose>

</xsl:template>


In the template where your column for the month will be rendered, you add two variables. You split from the value of the column the values for the year and the values for the month and save it into different variables. That will be used to call your new template which we created earlier.



<xsl:variable name="month" select="substring-after($thisNode/@*[name()=current()/@Name], ' ')"></xsl:variable>
<xsl:variable name="year" select="substring-before($thisNode/@*[name()=current()/@Name], ' ')"></xsl:variable>


Now it’s time to call the template where the values should be displayed and transfer the variables as parameters to it.



<xsl:call -template name="get-month-name">
<xsl:with -param name="month" select="$month"></xsl:with>
<xsl:with -param name="year" select="$year"></xsl:with>
</xsl:call>


Keep in mind that we didn’t change the values which are saved – we just changed the display properties or the rendering of the values.

In this post you can see how to expand the normal xslt stylesheet with your custom xslt stylesheet (post). I think i should also post how to make it for external lists. Hope i got some time for it soon.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.