Custom date formats with XSLT

The default format of a date field is something like 2012-01-01 00:00:00 – this is dependent on the language of the SharePoint installation. It is a very common requirement to display a date field in a different format.

Example

 2012-01-01 00:00:00


Example code

<xsl:value-of select="@ArticleStartDate"/>


Solution

1. Open the ItemStyle.xsl in SharePoint Designer or your favorite editor.
2. To be able to use the FormatDate function, add the DDWRT name space reference in the top section.

xmlns:ddwrt=“http://schemas.microsoft.com/WebParts/v2/DataView/runtime”

3. Use the code below to display the date field in the location you want the date shown. Every language has a different country code. The example in this article uses the UK country code (1033).

<xsl:value-of select="ddwrt:FormatDateTime(string(@ArticleStartDate) ,1033 ,'dd-mm-yyyy')"/>


Result

 01-01-2012

There are many different ways to display dates, take a look on MSDN for all the format specifiers. The country codes (supported locale identifiers) can also be found on MSDN.

Disable mobile pages for SharePoint 2010

By default SharePoint 2010 redirects mobile users to the mobile pages. Unfortunately there is no setting in Central Administration to disable the mobile redirect function.

Solution

1. On the server open the compat.browse file.

C:inetpubwwwrootwssVirtualDirectories[sitename]App_Browserscompat.browse


2. Change for each type of mobile browser the IsMobileDivice redirect setting.

<capability name= “IsMobileDivice” value=“true” />

to

<capability name= “IsMobileDivice” value=“false” />

Spaces in XSLT

Ever had a problem with rendering spaces in XSLT? I have. Most text can be placed directly in XSLT; however, when the text contains only a space, nothing will be rendered.

Example

<xsl:value-of select="@ProjectName"/> <xsl:value-of select="@ProjectStatus"/>


Example result

 ProjectnameGood

 

Solution
Place the space between the text tags and the space will be rendered correctly.

<xsl:value-of select="@ProjectName"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@ProjectStatus"/>


Result

 Projectname Good