Sunday, March 13, 2011

Change Locale of SharePoint Site Programatically

While working with SharePoint sites, we can change the regional settings of the site by using user interface and that’s easy to be done
You can do it like
Click on Site Actions > Site Settings > Click on Regional Settings link under Site Administration
and this is the small code using which you as a developer can do
I have used hardcoding here to Initialize CultureInfo object but you can do customizations according to your need

I hope this helps some one J


namespace ChangeCulture
{
  class Program
  {
    static void Main(string[] args)
    {
      using (SPSite site = new SPSite("http://yoursite"))
      {
        try
        {
          using (SPWeb web = site.RootWeb)
          {
            ChangeCulture(web);
          }
         }
        catch (Exception ex)
        {
         Console.WriteLine(string.Format("{0}:{1}", "Error: ", ex.Message));
        }
       }
      }

    private static void ChangeCulture(SPWeb web)
    {

      if (web != null)
      {
        web.AllowUnsafeUpdates = true;

        //Initialize CultureInfo
        CultureInfo ci = new CultureInfo("en-US");

        if (ci != null)
        {
          string.Format("{0}{1}", "Processing ", web.Name);
          web.Locale = ci;
          web.Update();
        }

         web.AllowUnsafeUpdates = false;
       }

       if (web != null)
       {
         foreach (SPWeb _web in web.Webs)
         {
           ChangeCulture(_web);
         }
       }


     }
    }
}
 

1 comment: