Sample code to get label and value for OptionSet in CRM

Hi,

Just sharing a sample code that retrieves optionset data from CRM and also sorts it. We are using it to bind drop down in one of our custom SharePoint Application Page.


/// <summary>
/// The get global option sets.
/// </summary>
/// <param name="config">
/// The config.
/// </param>
/// <param name="globalOptionSetName">
/// The _global option set name.
/// </param>
/// <returns>
/// The <see cref="List"/>.
/// </returns>
public static List<XrmOptionSet> GetGlobalOptionSets(IOrganizationService config, string globalOptionSetName)
{
using (
XrmServiceContext context = SDKHelper.GetXrmServiceContext(
config.OrganizationServiceUrl,
config.Credentials))
{
RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest
{
Name =
globalOptionSetName
};

// Execute the request.
RetrieveOptionSetResponse retrieveOptionSetResponse =
(RetrieveOptionSetResponse)context.Execute(retrieveOptionSetRequest);

OptionSetMetadata retrievedOptionSetMetadata =
(OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();

List<XrmOptionSet> optionSetItems = new List<XrmOptionSet>();

// Add each item in OptionMetadata array to the ListItemCollection object.
foreach (OptionMetadata o in optionList)
{
XrmOptionSet xrmOptionSet = new XrmOptionSet();
xrmOptionSet.Label = o.Label.LocalizedLabels[0].Label;
xrmOptionSet.Value = o.Value.Value.ToString(CultureInfo.InvariantCulture);
optionSetItems.Add(xrmOptionSet);
}

optionSetItems.Sort(
delegate(XrmOptionSet thisItem, XrmOptionSet otherItem)
{
return thisItem.Label.CompareTo(otherItem.Label);
});

return optionSetItems;
}
}

/// <summary>
/// The get local option sets.
/// </summary>
/// <param name="config">
/// The config.
/// </param>
/// <param name="entityName">
/// The entity name.
/// </param>
/// <param name="optionSetAttributeName">
/// The option set attribute name.
/// </param>
/// <returns>
/// The <see cref="List"/>.
/// </returns>
public static List<XrmOptionSet> GetLocalOptionSets(
IOrganizationService config,
string entityName,
string optionSetAttributeName)
{
using (
XrmServiceContext context = SDKHelper.GetXrmServiceContext(
config.OrganizationServiceUrl,
config.Credentials))
{
// Create the Attribute Request.
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName =
entityName,
LogicalName =
optionSetAttributeName,
RetrieveAsIfPublished
= true
};

// Get the Response and MetaData. Then convert to Option MetaData Array.
RetrieveAttributeResponse retrieveAttributeResponse =
(RetrieveAttributeResponse)context.Execute(retrieveAttributeRequest);
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

List<XrmOptionSet> optionSetItems = new List<XrmOptionSet>();

// Add each item in OptionMetadata array to the ListItemCollection object.
foreach (OptionMetadata o in optionList)
{
XrmOptionSet xrmOptionSet = new XrmOptionSet();
xrmOptionSet.Label = o.Label.LocalizedLabels[0].Label;
xrmOptionSet.Value = o.Value.Value.ToString(CultureInfo.InvariantCulture);
optionSetItems.Add(xrmOptionSet);
}

optionSetItems.Sort(
delegate(XrmOptionSet thisItem, XrmOptionSet otherItem)
{
return thisItem.Label.CompareTo(otherItem.Label);
});

return optionSetItems;
}
}
}

&nbsp;

 

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

1 thought on “Sample code to get label and value for OptionSet in CRM”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s