SharePoint 2010 Locale Stapler / Master Locale

I’ve just put up another project on CodePlex. As usual, you can download the full project and source code for SharePoint 2010 Locale Stapler.

Why?

SharePoint doesn’t let you set a master locale per web application. SharePoint 2010 Publishing Features lets you specify that all sub sites inherit the locale settings from the parent… But this is sadly lacking.

How?

My project gives you a SharePoint farm solution with a couple of feature staplers (FeatureSiteTemplateAssociations) that fire for all templates (“GLOBAL”) at either the farm or web application level. On creation of a new site collection or site, the feature staple fires and looks up the regional settings of the root web at the root site collection for the web application, and then copies those settings to the current web (SharePoint site).

 

Manage Farm Features or Web Application Features

Code!

First off, here’s the code for the payload – the method to do the copying.

/// <summary>
/// Sets the locale for web.
/// </summary>
/// <param name="web">The web.</param>
public static void SetLocaleForWeb(SPWeb web)
{
    try
    {
        // find the Uri of the current web
        Uri currentWebUri = new Uri(web.Url);
        // work out the Uri for the root (e.g. 
        // http://webapp1/sites/site1/subsite1 becomes http://webapp1)
        UriBuilder rootWebUri = new UriBuilder(currentWebUri.Scheme, 
            currentWebUri.Host, currentWebUri.Port);
        string rootSiteUrl = rootWebUri.ToString();
        // attempt to change the locale / regional settings by running as system...
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            // ...to access the root site/web of the web app
            using (SPSite rootSite = new SPSite(rootSiteUrl))
            {
                using (SPWeb rootWeb = rootSite.RootWeb)
                {
                    // ...and change the locale and regional settings of the current site
                    // to those of the root site collection root web's.
                    web.Locale = (System.Globalization.CultureInfo)rootWeb.Locale.Clone();
                    web.Update();
                    web.RegionalSettings = new SPRegionalSettings(rootWeb);
                    web.Update();
                }
            }
        });
    }
    catch (Exception ex)
    {
        LogError(System.Reflection.MethodBase.GetCurrentMethod().Name, 
            ex.Message, ex.StackTrace);
    }
}

And here’s the code that’s run on FeatureActivated at the Web level (and a similar one at the Site level):

/// <summary>
/// Occurs after a Feature is activated.
/// </summary>
/// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"/>
/// object that represents the properties of the event.</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    if (properties.Feature.Parent is SPWeb)
    {
        SPWeb web = (SPWeb) properties.Feature.Parent;
        Utilities.SetLocaleForWeb(web);
    }
}

Next, we need to create a a feature to do the stapling itself. This should include an element manifest (or elephant) with FeatureSIteTemplateAssociation nodes.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!--Web-level feature-->
  <FeatureSiteTemplateAssociation 
    Id="dd5923f9-05a4-4d6a-a78f-0dd8b08ff079" 
    TemplateName="GLOBAL" />
  <!--Site-level feature-->
  <FeatureSiteTemplateAssociation 
    Id="54fa6d42-c145-49dc-b2eb-f5db548bc111" 
    TemplateName="GLOBAL" />
</Elements>

Once again, don’t forget the full project and source code is available on CodePlex here: http://slash.codeplex.com.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>