A few months ago we had an issue where all test users had US regional settings, which was a bit of a problem since the users could not change their own settings, customer requirement, so I used the method below to set it right.
Two things to bear in mind:
- If you are looping through all users, make sure that you filter out SYSTEM and INTEGRATION users, better safe than sorry.
- You can avoid this (wrong regional settings) by setting the system regional settings to your desired setting before adding the users (not an option if an upgrade, but it's worth bearing in mind).
It might be a good idea just to pass the user id rather than the whole entity, but there you go.
private void SetUserSettingsToUK(IOrganizationService service, Entity user) { Guid userId = new Guid(); RetrieveUserSettingsSystemUserRequest settingsReq = new RetrieveUserSettingsSystemUserRequest(); RetrieveUserSettingsSystemUserResponse settingsRsp = new RetrieveUserSettingsSystemUserResponse(); settingsReq.ColumnSet = new ColumnSet(true); UpdateUserSettingsSystemUserRequest settingsUpdReq = new UpdateUserSettingsSystemUserRequest(); UpdateUserSettingsSystemUserResponse settingsUpdRsp = new UpdateUserSettingsSystemUserResponse(); Entity userSettings = new Entity("usersettings"); userId = user.Id; settingsReq.EntityId = userId; settingsReq.ColumnSet = new ColumnSet(true); settingsRsp = (RetrieveUserSettingsSystemUserResponse)service.Execute(settingsReq); userSettings = (Entity)settingsRsp.Entity; //helplanguageid appears not to be set so we don't check for it. if (userSettings.Attributes["currencysymbol"].ToString() != "£" || Convert.ToInt32(userSettings.Attributes["localeid"]) != 2057 || userSettings.Attributes["timeformatstring"].ToString().ToLower() != "hh:mm" || userSettings.Attributes["dateformatstring"].ToString().ToLower() != "dd/mm/yyyy" || Convert.ToInt32(userSettings.Attributes["uilanguageid"]) != 1033 || Convert.ToInt32(userSettings.Attributes["longdateformatcode"]) != 1 || Convert.ToInt32(userSettings.Attributes["negativecurrencyformatcode"]) != 1) { userSettings.Attributes["dateformatstring"] = "dd/MM/yyyy"; userSettings.Attributes["timeformatstring"] = "HH:mm"; userSettings.Attributes["currencysymbol"] = "£"; userSettings.Attributes["negativecurrencyformatcode"] = 1; userSettings.Attributes["longdateformatcode"] = 1; userSettings.Attributes["localeid"] = 2057; userSettings.Attributes["helplanguageid"] = 1033; userSettings.Attributes["uilanguageid"] = 1033; settingsUpdReq.Settings = userSettings; settingsUpdReq.UserId = userId; try { settingsUpdRsp = (UpdateUserSettingsSystemUserResponse)service.Execute(settingsUpdReq); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("Unable to set user settings for user {0}.", user["fullname"])); sb.AppendLine(string.Format("Exception:{0}.", ex.Message)); if (ex.InnerException != null) { sb.AppendLine(string.Format("InnerException: {0}", ex.InnerException.Message)); } log.Debug(sb.ToString()); log.Debug(sb.ToString()); } } }
No comments:
Post a Comment