Mostafa Moatassem
Mostafa Moatassem

Reputation: 351

Get current user language using javascript

In CRM 2011 it's easy to get current user language using javascript, using the following code:

Xrm.Page.context.getUserLcid();

Is there anyway to do the same in server side using plugins ?

Thanks and best regards

Upvotes: 0

Views: 5310

Answers (3)

Nicknow
Nicknow

Reputation: 7234

Here is a sample in a Plugin:

class GetUserLanguage : IPlugin
{

    public void Execute(IServiceProvider serviceProvider)
    {

        //PluginSetup abstracts setup code: http://nicknow.net/dynamics-crm-2011-abstracting-plugin-setup/
        var p = new PluginSetup(serviceProvider);

        var user = p.Context.InitiatingUserId;

        var lcid = RetrieveUserUiLanguageCode(p.Service, user);

    }

    //From the SDK: http://msdn.microsoft.com/en-us/library/hh670609.aspx
    private static int RetrieveUserUiLanguageCode(IOrganizationService service, Guid userId)
    {
        var userSettingsQuery = new QueryExpression("usersettings");
        userSettingsQuery.ColumnSet.AddColumns("uilanguageid", "systemuserid");
        userSettingsQuery.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userId);
        var userSettings = service.RetrieveMultiple(userSettingsQuery);
        if (userSettings.Entities.Count > 0)
        {
            return (int)userSettings.Entities[0]["uilanguageid"];
        }
        return 0;
    }
}

Upvotes: 2

Adi Katz
Adi Katz

Reputation: 548

I think that once the user selects a preferred locale you can pick it up using:

Thread.CurrentUICulture.LCID

Upvotes: -1

Nick
Nick

Reputation: 353

There are a couple options for it that I've used before.

  1. Use a Retrieve on the systemuser entity using the userid from the plugin execution context. Don't forget to set the column set to just get the user! Otherwise systemuser can be a hefty retrieve for online envs.

  2. Issue a WhoAmI request to the server.

I personally use the former because Retrieves are very common for devs who use plugins, but not all are familiar with the WhoAmI message (it derives from retrieve: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.whoamirequest.aspx)

Unfortunately I have not found a way to get it without issuing a request to the server. Let me know if this helps!

Nick

Upvotes: 0

Related Questions