How to get picklist option value with localized labels

Sometimes we use in the system can have multiple language options.
In such a situation it may be necessary to show the information in the interface according the to the language options.
Picklist was appropriate in this case we will see how to use multiple languages.

 


//Creating crm service

CrmServiceClient crmSvc = null;
crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("***", "***", "***"), AuthenticationType.AD, "***", "443", "***", useUniqueInstance: false, useSsl: true);

string logicalName = "my_logicalname";
string entityLogicalName = "my_entityname";

//Get metadata for localization

EntityMetadata entityMetadata = crmSvc.GetEntityMetadata(entityLogicalName, EntityFilters.All);
AttributeMetadata attribute = entityMetadata.Attributes.FirstOrDefault(z => z.LogicalName.Equals(logicalName));

if (attribute!= null && attribute.AttributeType == AttributeTypeCode.Picklist)
{
var options = (((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(attribute))).OptionSet.Options;
List<PickList> picklist = new List<PickList>();

picklist = options.ToList().ConvertAll(z => new PickList {
Label = z.Label.UserLocalizedLabel.Label,
Value = z.Value.Value.ToString()
});
}

public class PickList
{
public String DefaultValue { get; set; }
public String Value { get; set; }
public String Label { get; set; }
}

Another way

RetrieveEntityRequest rer = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Attributes,
LogicalName ="entitylogicalname"
};

RetrieveEntityResponse reres = (RetrieveEntityResponse)service.Execute(rer);

AttributeMetadata attribute = reres.EntityMetadata.Attributes.FirstOrDefault(z => z.LogicalName.Equals("logicalname"));

if (attribute != null && attribute.AttributeType == AttributeTypeCode.Picklist)
{
 var options = (((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(attribute))).OptionSet.Options;
 List<MyClass> picklist = new List<MyClass>();

 picklist = options.ToList().ConvertAll(z => new MyClass
 {
 Text = z.Label.UserLocalizedLabel.Label,
 Value = z.Value.Value.ToString()
 });

}

 

 

#csharp-picklist, #dynamaics-crm-localization, #picklist, #user-localized-label