Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

06 April 2011

How to retrieve labels from an optionset attribute for different languages.

In this post I will show how to retrieve optionset labels and descriptions from CRM 2011 for different languages.
I assume that you already have the translated labels for the optionset you want to retrieve. I will show how to create different labels for different languages in another post.

Here is the code to retrieve labels and descriptions for one particular language or all languages if you wanted.

public List<yourObject> GetAdditionalentitlementList(int languageCode){List<yourObject> yourObjectList= new List<yourObject>();           

yourObject yourObject;
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest 
             {
                 EntityLogicalName = Contact.EntityLogicalName,
                 LogicalName = "yourAttributeName",
               

                 RetrieveAsIfPublished = true
             };
// Execute the request.         
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)_orgService.Execute(retrieveAttributeRequest);           
// Access the retrieved attribute. 
 Microsoft.Xrm.Sdk.Metadata.
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
 (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata) 
            retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
       foreach (OptionMetadata oMD in optionList)
            {
                yourObject= new yourObject();
                foreach (var item in oMD.Label.LocalizedLabels)
                {
                    if (item.LanguageCode == languageCode)
                    {
                        yourObject.Label = item.Label;
                        yourObject.Value = oMD.Value.Value;
                       
                        break;
                    }
                }
                foreach (var description in oMD.Description.LocalizedLabels)
                {
                    if (description.LanguageCode == languageCode)
                    {
                        yourObject.Description = description.Label;
                        break;
                    }
                }
                yourObjectList.Add(yourObject);
            }

            return yourObjectList;
}



public class yourObject
{       public string Label { get; set; }
        public int Value { get; set; }
        public string  Description { get; set; }
        public override string ToString()
        {
            return this.Label;
        }
}


I hope this helps someone!!!

1 comment: