Populate OptionSet value from MS CRM 2011 into DropDownList in ASP.NET

Posted: March 24, 2013 in CRM 2011, CRM5, Microsoft Dynamics CRM
Tags: , , , ,

We had a requirement to create CRM record from custom ASP.NET page. There was an OptionSet on CRM Form which we need to display the option value in DropDownList of ASP.NET page. Below is the sample code to populate OptionSet value into DropDownList.

// Create Dictionary object to store the OptonSet value.
Dictionary<int, string> dropDownSource = new Dictionary<int, string>();

// Create Retrieve reuest
RetrieveAttributeRequest optionSetRequest = new RetrieveAttributeRequest();
optionSetRequest.EntityLogicalName = “lead”;
optionSetRequest.LogicalName = “leadqualitycode”;
optionSetRequest.RetrieveAsIfPublished = true;

RetrieveAttributeResponse optionSetResponse = (RetrieveAttributeResponse)service.Execute(optionSetRequest);

// Access retrive metadata

PicklistAttributeMetadata retrieveOptionSetMetadata = (PicklistAttributeMetadata)optionSetResponse.AttributeMetadata;

//get current optionlist for attribute

OptionMetadata[] optionList = retrieveOptionSetMetadata.OptionSet.Options.ToArray();

foreach (OptionMetadata o in optionList)
{
dropDownSource.Add(int.Parse(o.Value.ToString()), o.Label.UserLocalizedLabel.Label.ToString());
}

// ddlRating is object of dropdownlist
ddlRating.DataSource = dropDownSource;
ddlRating.DataBind();

Advertisements

Leave a Reply

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