04 April 2013

Get The CRM 2011 Timezone Settings For Current User With JavaScript

Sometimes it's useful to know the current user's timezone settings, for instance DateTime results in ODATA will be UTC so you may need to know how that should appear to the user. There are a bunch of blogs/user group posts about working out the summertime offset which doesn't make sense in my testing, if anybody can explain it and give examples I'd be grateful. Also a word of caution I've also noticed that if the Windows and CRM timezone settings are different it can also cause you some fun and games. Anyways enough of that here's the JavaScript to get the user's timezone settings:
GetUserTimeZoneDetails = function ()
{
 
 var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
 var filter = "/UserSettingsSet(guid'" + Xrm.Page.context.getUserId() + "')";
 
 var userSettingReq = new XMLHttpRequest();
 userSettingReq.open('GET', ODataPath + filter, false);
 userSettingReq.setRequestHeader("Accept", "application/json");
 userSettingReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 userSettingReq.send(null);

 var userSetting = this.JSON.parse(userSettingReq.responseText).d;
 var timeZoneBias = 0;
 try
 {
  timeZoneBias = userSetting.TimeZoneBias;
 }
 catch (e)
 {
  timeZoneBias = 0;
 }

 var timeZoneDayLightBias = 0;

 try
 {
  timeZoneDayLightBias = userSetting.TimeZoneDaylightBias;
 }
 catch (e)
 {
  timeZoneDayLightBias = 0;
 }
 
 return [timeZoneBias, timeZoneDayLightBias];
}
The function returns an array containing two elements the timezone bias and timezone daylight bias. The timezonebias is the local time zone adjustment for the user. System calculated based on the time zone selected. The timezonedaylightbias is the local time zone daylight adjustment for the user. System calculated based on the time zone selected. The SDK has details about other user settings that can be retrieved in the same way as above, see User Settings MetaData

No comments:

Post a Comment