TimeZones


Table of Contents

Had a requirement to staple a feature to site templates so that despite the server living in perth, east coast users would have their time-zone set correctly for the site they were creating.

using Microsoft.SharePoint

namespace TimeZoneFeature
{
    public class TimeZoneReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SetTimeTimeZoneId(properties);
        }

        private static void SetTimeTimeZoneId(SPFeatureReceiverProperties properties)
        {
            using (var webSite = (SPWeb) properties.Feature.Parent)
            {
                if (webSite.IsRootWeb)
                {
                    return;
                }

                string url = webSite.Url;
                var regionalSettings = new SPRegionalSettings(webSite);

                SPTimeZone timeZone = regionalSettings.TimeZone;

                int tz = DeterminTimeZoneFromUrl(url);

                if (tz != timeZone.ID)
                {
                    timeZone.ID = (ushort) tz;
                    webSite.Update();
                }
            }
        }
    }
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.