Reading Optionset using CRM 2011 IOrganizationService
Hi,
Below is the code to read “Optionset” using CRM 2011 IOrganizationService.
Below logic reads the Optionset and returns “KeyValue” list (Key = Option Label; Value = Option Value)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
public List<KeyValuePair<string, int>> GetOptionSet(string entityName, string attributeName, IOrganizationService service)
{
var optionSet = new List<KeyValuePair<string, int>>();
RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = entityName;
request.MetadataId = Guid.Empty;
request.LogicalName = attributeName;
request.RetrieveAsIfPublished = true;
OrganizationResponse response = service.Execute(request);
if (response != null && response.Results != null)
{
foreach (KeyValuePair param in response.Results)
{
foreach (OptionMetadata option in ((EnumAttributeMetadata)param.Value).OptionSet.Options)
{
string name = option.Label.LocalizedLabels[0].Label.ToString().Trim();
int value = option.Value.Value;
optionSet.Add(new KeyValuePair(name, value));
}
}
}
return optionSet;
}
// Returns option label by value
public string GetOptionLabelByValue(List<KeyValuePair<string, int>> optionSet, int optValue)
{
foreach (KeyValuePair< string, int> optn in optionSet)
{
if (optn.Value == optValue)
{
return optn.Key;
}
}
return string.Empty;
}
// Returns option Value by Label
public int GetOptionValueByLabel(List<KeyValuePair<string, int>> optionSet, string optLabel) { {
foreach (KeyValuePairoptn in optionSet)
{
if (optn.Key) == optLabel)
{
return optn.Value;
}
}
return 0;
}
How Do I call these methods:-
- To read ‘Gender (i.e., gendercode)’ optionset of entity ‘Contact (i.e., contact)’
- Get Optionset dictionary by calling “GetOptionSet()” method
- Call “GetOptionLabelName()” to get Label by Value
- Call “GetOptionValueByLabel()” to get Value by Label
// Get OptionSet First
var optGender = GetOptionSet(“contact”, “gendercode”, orgService);
// Get Option label by value
string optLblGander = GetOptionLabelName(optGender , 1); /* 1 is Value of Male */
// To get Option Value by Label
int optValueGander = GetOptionValueByLabel(optGender , “Male”);
Hope it helps 🙂
Leave a Reply Cancel reply
Stats
- 1,579,664 hits
Tweets
- RT @MikeFactorial: Big things are coming to the #CoEStarterKit. So, @ManuelaPichler_ and team are building the tension for the April releas… 2 weeks ago
- New blog post : Boost conversations using GPT in Power Virtual Agent (PVA) rajeevpentyala.com/2023/03/08/pow… 2 weeks ago
- RT @PPDevWeekly: 🔥 Going Live: Power Platform Dev Weekly 155 bit.ly/PPDevWeekly155 This week's cover story by @RajeevPentyala With gre… 2 weeks ago
- RT @caseyburke21: Pipelines is now generally available! Huge congratulations to the team and all who have supported reaching this milestone… 3 weeks ago
- New blog post : Call Dataverse actions directly in power-fx rajeevpentyala.com/2023/03/01/ste… 3 weeks ago
Top Posts
- Power Apps component framework (PCF) - Beginner guide
- [Experimental Feature] Call Dataverse actions directly in Power Fx
- [Step by Step] Power Apps | Show pop ups in Canvas App
- [Step by Step] Connecting to Azure SQL Server using OLEDB Connection from SSIS
- Auto generate new GUID for ‘uniqueidentifier’ column in SQL Table
- [Code Snippet] Custom Workflow Activity with Input and Output Params
- God Mode - Level Up - Dynamics 365 Chrome Extension
- Associate/Disassociate plugin messages in CRM
- Set “Created On”,” Created By”, “Modified On”, “Modified By” fields using SDK/Data Import/Plug-in – Dynamics 365
- Power Platform - Pass json collection from Canvas App to Power Automate
Thanks for this. However I can’t get this to compile – on this line: foreach (KeyValuePair param in response.Results){
I get “Using the generic type ‘System.Collections.Generic.KeyValuePair requires 2 type arguments.