Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
Just as a pro-tip: learn to the use and navigate MSDN object model documentation: msdn.microsoft.com/en-us/library/microsoft.sharepoint.aspx - and also, the Power Tools for Visual Studio 2010 - msdn.microsoft.com/en-us/vstudio/bb980963- will help you dig out parts of the object model you want to find. –  James Love Feb 2 '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 3 '11 at 9:33
add comment

2 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
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! –  Matt Feb 3 '11 at 14:52
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.