Retrieve Normal(Local) Option Set Text
// Get Normal option set Text
string optionsetText = entity.FormattedValues["new_optionset"];
or
string optionsetText = entity.GetFormattedAttributeValue("new_optionset");
Retrieve Global Option Set Text
int OptionsetValue = ((Microsoft.Xrm.Sdk.OptionSetValue)entity["new_localoptionset"]).Value;
string GlobaloptionsetText= GetOptionsetText(entity, service, "new_globaloptionset", OptionsetValue );
// Retrieves Global Option set Selected Text
// Parameters: 1. Entity Name 2. Service 3. Global Option Set Name 4. optionset selected value
public string GetOptionsetText(Entity entity, IOrganizationService service,string optionsetName,int optionsetValue)
{
string optionsetSelectedText = string.Empty;
try
{
RetrieveOptionSetRequest retrieveOptionSetRequest =
new RetrieveOptionSetRequest
{
Name = optionsetName
};
// Execute the request.
RetrieveOptionSetResponse retrieveOptionSetResponse =
(RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);
// Access the retrieved OptionSetMetadata.
OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();
foreach (OptionMetadata optionMetadata in optionList)
{
if (optionMetadata.Value == optionsetValue)
{
optionsetSelectedText = optionMetadata.Label.UserLocalizedLabel.Label.ToString();
break;
}
}
}
catch (Exception)
{
throw;
}
return optionsetSelectedText;
}
===========================================
// Gets Local/Normal optionset Lable using Metadata Service
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;
}
happy coding :)
Thank you Guru! Your code helped me with this OptionSet nightmare!
ReplyDeleteThis is great! How would you get the lable for a Two Options field type? IOW, if the value is 0, how do you know if the label is False, No, Empty, etc?
ReplyDeleteGary! you can use Metadata service request to retrieve any attribute data. we can get Label of Two option filed Label as well, i did not tried it.
ReplyDeleteHi Guru,
ReplyDeleteCan you please let us know how to retrieve the Text of TwoOptions?
Hi Pradeep,
Deleteuse the following code to get two options field Text
// Get Normal two option set Text
string twoOptionLable = entity.FormattedValues["new_twoOptionSet"];
Let me know if any issues..
And how can I retrieve optionset's selected value for record into variable from its Post Image in Plugin created on Update event ?
ReplyDeletevar optvalue = PostImage.Attributes["new_optionset"];
DeleteHi Guru,
ReplyDeleteAccount has accountratingcode optionset. I want to retrieve value of that optionset when accountratingcode is on unassigned.
Hi Ravideep,
Deleteyou can use the following code to get the optionset value inside the plugin
var optvalue = entity.Attributes["accountratingcode"];
I did not get what exactly your requirement. provide me more information where you wants to get optionset value?(plugin,javascript etc.) so that i can help you.
Thanks,
Guru
How to retrieve and update Two Option Option set?
Deleteuse the following code to retrieve two option set text
Delete// Get Normal two option set Text
string twoOptionLable = entity.FormattedValues["new_twoOptionSet"];
//to update two option field
entity.Attributes.Add("new_bitfield", true);
Hope it helps!
Great post... work like charm
ReplyDeleteHi Guru
ReplyDeleteWould you have any insight as to how to retrieve the optionset text with an OData query?
I need to create reportes with PowerQuery, but whenever an Entity has picklists, the OData query just only the value associated.
Any help will be greatly appreciated.
Regards, Ariel
1.How can we get input of Option set text value in custom workflow?
ReplyDelete2.How can we set output of Option set text value in custom workflow?
can anyone plz help me to resolve this problem..
I am retrieving audit value to astring using the code-> String Value = attributeDetail.OldValue[attribute.Key].ToString();
ReplyDeleteIf the value is not string I am getting "Microsoft.Xrm.Sdk.OptionSetValue". I want to get the optionset value in string variable as mentioned above.