Question

How can we set the locale of a sharepoint site for a particular user according to his/her system locale through coding?

Edit: Specifically, I've created a list through coding. Suppose that I've added an item to that list. Then I've changed the locale. Then again when I'm trying to add another item within than list it throws an exception regarding the date format as it has already been changed.

Était-ce utile?

La solution

Try:

 using (SPSite site = new SPSite("http://sp"))
{
    try
    {
        using (SPWeb web = site.RootWeb)
        {
            if (web != null)
            {
                web.AllowUnsafeUpdates = true;
                CultureInfo culture = new CultureInfo("en-US");
                if (culture != null)
                {
                    web.Locale = culture;
                    web.Update();
                }
                web.AllowUnsafeUpdates = false;
            }
        }
    }
    catch (Exception ex)
    {
        // Log exception
    }
}

Autres conseils

The general Regional Settings you can set via the SPWeb object: SPWeb.Locale. The per user setting is stored with the SPUser object, specifically SPuser.RegionalSettings like explained here.

Edit: You can get the locale of the current user by getting the current SPUser via SPWeb.CurrentUser, then again you can get the RegionalSettings from this user.

I'm not sure how you can get the system locale of the current user via code behind stuff - it would be possible via Javascript and make changes based on that.

Depending on your requirements will depend on which language you use and how you implement this.

You can update the regional settings for a web site using PowerShell by using SPWeb.Locale property. This could be used by a script which configures new sites automatically.

This site describes SPWeb.Locale: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.locale.aspx

This site demonstrates updating the regional settings using a simple PowerShell script: http://blogs.technet.com/b/lukeb/archive/2011/04/21/sharepoint-and-setting-regional-settings-in-powershell.aspx

EDIT:

I only just read the tag with sharepoint-online. Currently PowerShell can't be used remotely for SP online. There is plenty of discussion about if/when this will occur. You can, however, use web services to administer the site. You can connect via Web Services to PowerShell using the New-WebServiceProxy cmdlet.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top