2
votes

can someone help me to figure out how I can retrieve with the CRM 2011 SDK different languages for the label of one optionset?

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. Has my contact a English country, the sdk should bring me back the English label and so on..

Get the value is no problem:

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

But how can I get the label in the correct language?

4

4 Answers

4
votes

You'll need to perform a RetrieveAttributeRequest to get the EnumAttributeMetadata, then lookup the correct value based on the language code:

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;

I tend to like to cache the metadata rather than hitting the CRM server constantly for the text information. 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. As long as you've added the System.Linq namespace in a using statement, any IEnumerable will have it.

The German language is located 1031. Although the more correct route would be to lookup the user's UsersSetting.UILanguageId. I believe that should contain the correct code, although I haven't tested it...

0
votes

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.

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

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;

}