Tuesday, June 11, 2013

Get Current User's Regional Settings and Locale

SharePoint has a feature where user can set his/her own Regional settings and locale. In this post I will tell you how to get these settings using Object Model and show the date format according to the user's Locale.

To set your Regional settings

Select Welcome Control-> Select My Settings -> Select My Regional Settings

You have to uncheck the "Always follow web settings" option and then set your Locale.

In the code file, use this code

Get the current user's Regional settings by SPRegional Settings class

 SPRegionalSettings rgnUser = SPContext.Current.Web.CurrentUser.RegionalSettings;
             
I have created a property to get the date format you can use the same

public static string format
        {
            get
            {
                SPRegionalSettings rgnUser = SPContext.Current.Web.CurrentUser.RegionalSettings;
                if(rgnUser!=null)
             
                return "{0:" + System.Globalization.CultureInfo.GetCultureInfo(Convert.ToInt32(rgnUser.LocaleId)).DateTimeFormat.ShortDatePattern + "}";

                else

                    return "{0:" + System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern + "}";
            }
        }
If user has set his/her Regional Settings then it will show the date format according to the set locale otherwise it will show the current culture's date format.