2

I've been going crazy trying to find where I can access the Locale found by going to your username at the top right of SharePoint -> My Settings -> My Regional Settings

alt text

I've been able to access Theme by using this code:

        // Get data from SharePoint.
        SPWeb web = SPContext.Current.Web;
        SPList siteUserInfoList = web.Site.RootWeb.SiteUserInfoList;
        SPUser user = web.CurrentUser;
        SPListItem currentUserItem = siteUserInfoList.GetItemById(user.ID);

        string theme = (string)currentUserItem["Theme"];
        //string locale = web.Locale.Name;
        //string locale = web.Site.RootWeb.Locale.Name;
        //string locale = (string)currentUserItem["Locale"];

At the bottom of that code snippet, you can see three attempts at retrieving Locale, but they do not work. Any help is appreciated; I've been wasting a lot of time not finding much with foreach loops through all the SPList, SPListItem, etc objects... :(

* Feb 3, 2011 *

Code is pointed out in James Love's answer below for successfully retrieving Locale from "My Regional Settings".

However, this only works when "Always follow web settings" is NOT checked for Follow Web Settings. (See top of attached picture.)

// Get the locale.
string locale = "a";
try
{
    CultureInfo ci = new CultureInfo((int)web.CurrentUser.RegionalSettings.LocaleId);
    locale = ci.Name;
}
catch
{
    try { var locale2 = web.CurrentUser.RegionalSettings.LocaleId; }
    catch { locale = "en-US"; };
}

The code where I have var locale2 will fail if "Always follow web settings" is checked, but otherwise it will work. That means that in the outermost catch block, I should set my locale variable to something else. I just need to find out where to access the overall locale settings.

  • Just as a pro-tip: learn to the use and navigate MSDN object model documentation: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.aspx - and also, the Power Tools for Visual Studio 2010 - http://msdn.microsoft.com/en-us/vstudio/bb980963- will help you dig out parts of the object model you want to find. – James Love Feb 02 '11 at 22:29
  • I also find Reflector an invaluable tool. If I can see a page setting a property, I can normally use Reflector to track through to see how that property is set.

    (Except when it's a call into COM code)

    – Andy Burns Feb 03 '11 at 09:33

2 Answers2

4

Tried SPContext.Current.Web.CurrentUser.RegionalSettings.LocaleID?

James Love
  • 25,512
  • 2
  • 45
  • 77
  • Yes, this is what I needed. I just had to add a little extra code to change LocaleID from an unsigned int to the appropriate string for my use.

    CultureInfo ci = new CultureInfo((int)web.CurrentUser.RegionalSettings.LocaleId); string locale = ci.Name;

    Now when I change the locale as shown in the picture in the original post, the page with this code gets updated as well. So it works, thanks!

    – Matthew Steven Monkan Feb 03 '11 at 14:52
0

In addition here is a way of overwriting the user's checkbox. (by overwriting their regional settings object)

SPRegionalSettings regionalSettings = new SPRegionalSettings(SPContext.Current.Web);
regionalSettings = SPContext.Current.Web.CurrentUser.RegionalSettings;

regionalSettings.LocaleId = uint.Parse(ddlLanguage.SelectedItem.Value);

SPContext.Current.Web.CurrentUser.RegionalSettings = regionalSettings;

Then there is no need to worry about the box being checked and the user's other settings stay intact.

Ryan Erickson
  • 1,941
  • 2
  • 21
  • 32