Set the Locale of a Dynamics CRM Organization and all users

The default locale of Dynamics CRM is of course English (United States) so one of the first things those of us in the rest of the world need to do when provisioning a new Organization (I know, with a ‘z’) or CRM Online instance is to change this.

Using the SDK this is very easy, and it’s also possible to change the locale of every user at the same time, very helpful if you’ve just imported 500 users then sighed when you noticed wrong-way-round dates.

Here’s the code:

public static void SetLocale(IOrganizationService orgService, int localeId = 2057, bool updateExistingUsers = true)
    // Get the Organization Entity, if it doesn't already have the supplied locale
    Trace.WriteLine(string.Format("Setting Organization Locale to '{0}'", localeId));
    QueryExpression query = new QueryExpression("organization");
    query.Criteria.AddCondition("localeid", ConditionOperator.NotEqual, localeId);
    Entity orgEntity = RetrieveSingleEntity(orgService, query);

    // If the Organization Entity is returned, update its locale
    if (orgEntity != null)
        orgEntity["localeid"] = localeId;

    // If update of user locales is not required, return
    if (!updateExistingUsers) return;

    // Retrieve all "usersettings" Entities which do not already have the supplied locale
    Trace.WriteLine(string.Format("Setting Locale of all existing Users to '{0}'", localeId));
    query = new QueryExpression("usersettings");
    query.Criteria.AddCondition("localeid", ConditionOperator.NotEqual, localeId);

    // Loop through and set each to the supplied locale
    foreach (Entity userSettingsEntity in orgService.RetrieveMultiple(query).Entities)
        userSettingsEntity["localeid"] = localeId;

Oh and my RetrieveSingleEntity method, which is referenced above:

public static Entity RetrieveSingleEntity(IOrganizationService orgService, QueryExpression query)
    query.PageInfo = new PagingInfo { Count = 1, PageNumber = 1 };
    EntityCollection results = orgService.RetrieveMultiple(query);
    return (results.Entities == null || results.Entities.Count < 1) ? null : results.Entities[0];

Leave a Reply

Your email address will not be published. Required fields are marked *