Wednesday, 20 January 2010

Problem and Fix: Microsoft.SharePoint.WebControls.SPDatePickerControl doesn't render Dates based on Regional Settings

I had a problem today that a SharePoint Application Page (ie an ASP.NET page hosted in the SharePoint _layouts directory under the 12 hive) I use wasn’t rendering the SharePoint Date Time Control date format according to SharePoint regional settings.  No matter what setting I tried, it would not change the format that was rendered - instead it was using the default US date format. This is disconcerting for users because all the other pages in the site render the dates correctly. Even the InfoPath forms hosted via Forms Services (which make use of the Application pages as lookup forms) render the date format as per Australian regional settings.

 Some of the things I tried to resolve/check the issue:

  1. Regional Settings under Site Settings -> Regional Settings did not make any difference.

  2. Checked Internet Explorer 7.0 browser settings in Tools->Options->Language – which is already English [AU]

  3. I tried to override the InitializeCulture() handler and set the Thread.Culture (ie date format etc) and UICulture (which resource files that ASP.NET uses) – but it made no difference.

    /// 
            /// Force application page to render in Australian format as SP Date Control
            /// support standard SharePoint regional Settings
            /// 
            protected override void InitializeCulture()
            {
                Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture("en-AU");
    
                Thread.CurrentThread.CurrentUICulture = new
                            CultureInfo("en-AU");
                base.InitializeCulture();
            }
    


However none of the solutions above did a thing. By luck I noticed through debugging that there is a property called “LocaleId” on the Microsoft.SharePoint.WebControls.SPDatePickerControl ( as per http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatepickercontrol.aspx). When I set this to the LocaleId=3081 (for English(Australian)), then the SharePoint Date control renders the date format correctly (ie in dd/MM/yyyy format for Australian users rather than the default US date format of MM/dd/yyyy).

This LocaleId could also be set programatically based on a config file if you don't want to hardcode the locale into your SharePoint Application Page.


1 comment:

Hassan said...

Great, just observed this in a demo and your blog saved my time from researching and finding the solution myself