Blog Post

Microsoft SharePoint Blog
2 MIN READ

Setting Time Zone for a user in SharePoint 2013

SPDev_Support's avatar
SPDev_Support
Icon for Microsoft rankMicrosoft
May 01, 2019
First published on TECHNET on Nov 04, 2014

This post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team

With SharePoint 2010 you can set time zone for a user using code like this:

// Code1
using (SPSite cSite = new SPSite(siteURL))

{

using (SPWeb curWeb = cSite.RootWeb)

{

curWeb.AllowUnsafeUpdates = true;

SPUser editUser = curWeb.EnsureUser(loginName);

SPRegionalSettings userRegSettings = new SPRegionalSettings(curWeb, true);

userRegSettings.TimeZone.ID = 10; // see List of TimeZone ID

editUser.RegionalSettings = userRegSettings;

editUser.Update();

curWeb.AllowUnsafeUpdates = false;

}

}

After the code is executed, you can browse to a Document library to check a datetime column, for instance, Modified. It shows the datetime in the time zone you set.

However this is reported not working any more with SharePoint 2013. The reason is that SharePoint 2013 sets default value of User Profile property “ SPS-RegionalSettings-Initialized ” to false, which is true in SharePoint 2010. You need first to set the property to true like this:

// Code2
using (SPSite ccSite = new SPSite(siteURL))

{

SPServiceContext serviceContext = SPServiceContext.GetContext(ccSite);

UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);

UserProfile updUser = userProfileMgr.GetUserProfile(loginName);

updUser["SPS-RegionalSettings-Initialized"].Value = true;

updUser.Commit();

}

If you execute the Code1 immediately after Code2 you will find that the user still see datetime column value (e.g. Modified) in old time zone.

This is because the timer job “ User Profile Service Application - User Profile to SharePoint Language and Region Synchronization ” (scheduled for 1 minute interval) has not completed yet. Setting ["SPS-RegionalSettings-Initialized"].Value to true will cause synchronizing language and region information from the User Profile service application to SharePoint users.

To set a time zone for a user on a site, first run Code2, set some time delay to allow timer job “User Profile Service Application - User Profile to SharePoint Language and Region Synchronization” to complete synchronization and then run Code1.

Updated Aug 27, 2020
Version 2.0
No CommentsBe the first to comment