While working for projects where users are located in different time zones…and The SharePoint server is located in one particular time zone, it becomes a tedious work when it comes to display or deal with events occurring across different timzones using code.
The Time Zone can be managed at three levels. Web Application level, Site Collection level and user Level.
If we wanna do a setting of Time zones to users’ current time zone using code, Its quite straight forward..Well not that much straight 🙂
Below code can be used to Set the time zone for a user once he logs into a page.
SPWeb webCurr = SPContext.Current.Web;
SPUser curruser = SPContext.Current.Web.CurrentUser;
string strReturn =
@”Time Zone: [TimeZone]“;
try
{
if (curruser.RegionalSettings == null)
{
SPRegionalSettings regsettings =
new SPRegionalSettings(webCurr, false);
ushort intTimeZoneID = GetTimeZoneID(strTimeZone);
if (intTimeZoneID != 0)
{
regsettings.TimeZone.ID = intTimeZoneID;
webCurr.AllowUnsafeUpdates = true;
curruser.RegionalSettings = regsettings;
curruser.Update();
webCurr.AllowUnsafeUpdates = false;
}
string strData = strReturn.Replace(“[TimeZone]”, strTimeZone);
return strData;
}
else
{
string strData = strReturn.Replace(“[TimeZone]”, curruser.RegionalSettings.TimeZone.Description);
return strData;
}
}
catch (Exception ex)
{
return “Error ” + ex.Message;
}
finally
{
webCurr = null;
curruser = null;
}
Considering that the strTimeZone is The time zone picked from the browser location using JavaScript.
This will work easily for any user ..ahhhh not all users…
We will get an Err0r ” Thread is aborted” which is not able to tell you what is wrong there..
anyways… Look for a group where you have assigned Read access to your user. A small change would be required for such users’ permission. Navigate to the permission properties…and check the checkbox for Edit personal user information in the list of available permission at Site
That’s it..You are good to go and the time zone would be set to all users based on their current location(time Zone).