原创地址:http://www.cnblogs.com/jfzhu/archive/2012/12/10/2812040.html
转载请注明出处
我在之前的博客中介绍过如何为Microsoft Dynamics CRM 2011 安装语言包,安装了不同的语言包后,用户可以选择使用不同的界面语言。我在本文中介绍一下如何获得当前用户使用的界面语言。
(一) 前端
如果是使用JScript获得当前用户使用的界面语言,比如在Form的OnLoad事件处理器中,可以用
context.getUserLcid()
该方法返回一个数字,即为Language ID,比如美国英语是1033,中文简体为2052。完整的Locale ID Values表,大家可以参见http://msdn.microsoft.com/en-us/library/ms912047(WinEmbedded.10).aspx
下面在account form的OnLoad事件中测试一下:
if (typeof (AccountLibrary) == "undefined") { AccountLibrary = { __namespace: true }; } AccountLibrary = { Name: "AccountLibrary", OnLoad: function () { var languageid = Xrm.Page.context.getUserLcid(); alert(languageid); } }
打开一个account,我们能看到弹出的languageid的对话框:
(二) 后端
运行在服务器上的代码(C#或者VB)该如何获得当前用户的界面语言呢?
首先要获得当前用户的ID,我们以插件(plugin)为例,当前用户为context.UserId。获得当前用户的ID之后该怎么办,在CRM 4中,我们使用RetrieveUserSettingsSystemUserRequest来获得用户设置(User Settings),但是在CRM 2011中,虽然这个Message还被支持,但这只是基于向后兼容的考虑,在SDK中清楚地写着该Message已经是Deprecated,我们应该使用RetrieveMultiple方法来获得相应的usersettings Entity,所以还在使用RetrieveUserSettingsSystemUserRequest的同学,最好改用RetrieveMultiple方法。下面我们在一个测试插件中来演示一下:
public class Plugin : IPlugin { #region IPlugin Members private int? GetCurrentUserLanguageId(IOrganizationService service, ITracingService tracingService, Guid userid) { QueryExpression mySavedQuery = new QueryExpression { ColumnSet = new ColumnSet("systemuserid", "uilanguageid"), EntityName = "usersettings", Criteria = new FilterExpression() { FilterOperator = Microsoft.Xrm.Sdk.Query.LogicalOperator.And, Conditions = { new ConditionExpression { AttributeName = "systemuserid", Operator = ConditionOperator.Equal, Values = {userid} } } } }; EntityCollection ec = service.RetrieveMultiple(mySavedQuery); tracingService.Trace("Debug: usersettings is retrieved"); if (ec != null && ec.Entities != null && ec.Entities.Count > 0 && ec.Entities[0].Contains("uilanguageid")) { tracingService.Trace("Debug: usersettings contains uilanguageid"); return (int)(ec.Entities[0]["uilanguageid"]); } else { tracingService.Trace("Debug: usersettings does not contain uilanguageid"); return null; } } public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Get a reference to the organization service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); // Get a reference to the tracing service. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); try { if (context.MessageName == "Create") { Entity entity = (Entity)context.InputParameters["Target"]; int? languageid = GetCurrentUserLanguageId(service, tracingService, context.UserId); throw new InvalidPluginExecutionException(string.Format("languageid = {0}", languageid.ToString())); } } catch (FaultException<OrganizationServiceFault> ex) { tracingService.Trace("Error: {0}", ex.ToString()); throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); } catch (Exception ex) { tracingService.Trace("Error: {0}", ex.ToString()); throw; } } #endregion }
创建一个account,可以看到抛处的异常,错误信息中包含了language id
总结:如果在前端获得当前用户的界面语言,可以用context.getUserLcid(),在后端可以用RetrieveMultiple方法来获得systemuserid为当前用户ID的usersettings Entity,然后获得usersettings Entity的uilanguageid。