Mr Lahey
Mr Lahey

Reputation: 676

Get the string value from OptionSetValue in CRM Plugin

I'm wondering how to get the string value of an OptionSet in a CRM plugin I am making. I thought all I had to do was pass the int value to OptionSetValue, but that doesn't seem to be working. Here is my code:

aBillingFrequencyCode = new OptionSetValue(myContract.BillingFrequencyCode.Value).ToString();

But the output is just

Microsoft.Xrm.Sdk.OptionSetValue

Any ideas?

Upvotes: 8

Views: 37327

Answers (3)

Vinod Srivastav
Vinod Srivastav

Reputation: 4263

That would be easy considering there is entity.FormattedValues["field_name"] you just need to identify the field type and get the string out of it.

var account = service.Retrieve("account",Guid.Parse(""),new ColumnSet(true));

var statcode = account.Attributes
    .Where(a => a.Value.GetType() == typeof(OptionSetValue) && a.Key == "statecode")
    .Select(f=> account.FormattedValues[f.Key])
    .First();

The above piece of code will identify Filter & fetch the statecode text from the account entity.

Upvotes: 1

Nicknow
Nicknow

Reputation: 7234

You can retrieve OptionSet labels without retrieving all of the entity's metadata. I've provided two methods. One will use the language code (LCID) of the account that IOrganizationService is running under. The other allows you to specify the LCID.

Note, if you are going to use these extensively in code you may want to consider caching the value to improve performance - this will depend on your specific applications requirements.

If you plan to retrieve these values for multiple Option Sets on a single entity at the same time you should use Guido's code above and retrieve all the entity metadata in one call to reduce the number of calls you are required to make to CRM. Hence, each of our code snippets is more efficient in certain circumstances.

//This method will return the label for the LCID of the account the IOrganizationService is using
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
{
    var attReq = new RetrieveAttributeRequest();
    attReq.EntityLogicalName = entityName;
    attReq.LogicalName = fieldName;
    attReq.RetrieveAsIfPublished = true;

    var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
    var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

    return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
}

//This method will return the label for the specified LCID
public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, int lcid, IOrganizationService service)
{
    var attReq = new RetrieveAttributeRequest();
    attReq.EntityLogicalName = entityName;
    attReq.LogicalName = fieldName;
    attReq.RetrieveAsIfPublished = true;

    var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
    var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

    return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.LocalizedLabels.Where(l => l.LanguageCode == lcid).FirstOrDefault().Label;
}        

Upvotes: 24

Guido Preite
Guido Preite

Reputation: 15138

In order to get the OptionSet text value you need to query the metadata (this because Dynamics CRM supports multiple languages)

Here an example:

public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
{
    string AttributeName = attributeName;
    string EntityLogicalName = entityName;
    RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
    {
        EntityFilters = EntityFilters.All,
        LogicalName = EntityLogicalName
    };
    RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
    Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
    Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
    Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
    IList<OptionMetadata> OptionsList = (from o in options.Options
                                            where o.Value.Value == optionSetValue
                                            select o).ToList();
    string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
    return optionsetLabel;
}

Upvotes: 7

Related Questions