简体   繁体   English

Dynamics CRM 2011 OptionSetValue用不同的语言

[英]Dynamics CRM 2011 OptionSetValue in different languages

can someone help me to figure out how I can retrieve with the CRM 2011 SDK different languages for the label of one optionset? 有人可以帮助我弄清楚如何使用CRM 2011 SDK检索一个选项集标签的不同语言吗?

My task is the following: For example, I have a contact with german language, then my sdk should bring me back the optionset value in that language. 我的任务如下:例如,我与德语联系,那么我的sdk应该带回该语言的optionsset值。 Has my contact a English country, the sdk should bring me back the English label and so on.. 如果我与某个英国国家/地区联系过,SDK应该将我带回英语标签,依此类推。

Get the value is no problem: 获取值没问题:

int optSetValue = ((OptionSetValue)entity["optionsetFieldName"]).value

But how can I get the label in the correct language? 但是如何获得正确语言的标签?

You'll need to perform a RetrieveAttributeRequest to get the EnumAttributeMetadata , then lookup the correct value based on the language code: 您需要执行RetrieveAttributeRequest以获得EnumAttributeMetadata ,然后根据语言代码查找正确的值:

string languageCode = germanLanguageCode; // Set
int optSetValue =  0; // Set
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
    EntityLogicalName = entityLogicalName,
    LogicalName = attributeName,
    RetrieveAsIfPublished = true
};

var response = (RetrieveAttributeResponse)service.Execute(attributeRequest);
var optionList = ((EnumAttributeMetadata)response.AttributeMetadata).OptionSet.Options;

return optionList.GetFirst(o => o.Value == optSetValue).Label.LocalizedLabels.First(l => l.LanguageCode == languageCode).Label;

Alternatively, if your service is running as the German user, then you can access the German text via return optionList.GetFirst(o => o.Value == optSetValue).Label.UserLocalizedLabel.Label; 或者,如果您的服务以德语用户身份运行,则可以通过return optionList.GetFirst(o => o.Value == optSetValue).Label.UserLocalizedLabel.Label;访问德语文本return optionList.GetFirst(o => o.Value == optSetValue).Label.UserLocalizedLabel.Label;

I tend to like to cache the metadata rather than hitting the CRM server constantly for the text information. 我倾向于缓存元数据,而不是经常访问CRM服务器以获取文本信息。 But then again, I'm in an English only org and don't have to worry about what language people are using... 但是话又说回来,我在一家只有英文的组织中,不必担心人们使用的是什么语言...

Additional Answers from Comments 评论中的其他答案

GetFirst() is just a standard Linq Method. GetFirst()只是一个标准的Linq方法。 As long as you've added the System.Linq namespace in a using statement, any IEnumerable will have it. 只要您在using语句中添加了System.Linq命名空间,任何IEnumerable都将拥有它。

The German language is located 1031 . 德语为1031 Although the more correct route would be to lookup the user's UsersSetting.UILanguageId. 尽管更正确的方法是查找用户的UsersSetting.UILanguageId。 I believe that should contain the correct code, although I haven't tested it... 我相信其中应该包含正确的代码,尽管我尚未对其进行测试...

To retrieve the user localized option label for the selected value, try 要检索所选值的用户本地化选项标签,请尝试

string myoption;

if (!entity.FormattedValues.TryGetValue("optionsetFieldName", out myoption))
{
    myoption = "Not found";
}

The IList<KeyValuePair<string,string>> FormattedValues can also be queried using LINQ. 也可以使用LINQ查询IList<KeyValuePair<string,string>> FormattedValues

string optionlabel =entity.FormattedValues["optionsetFieldName"];

the code working for me: 适用于我的代码:

public static dynamic GetOptionSet(string entityName, string fieldName, int langId, OrganizationServiceProxy proxy)
{
    RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest();
    retrieveDetails.EntityFilters = EntityFilters.All;
    retrieveDetails.LogicalName = entityName;

    RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)proxy.Execute(retrieveDetails);
    EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
    PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
    OptionSetMetadata options = picklistMetadata.OptionSet;
    var optionlist = (from o in options.Options
                          select new { Value = o.Value, Text = o.Label.LocalizedLabels.First(l => l.LanguageCode == langId).Label }).ToList();

    return optionlist;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM