Wednesday, February 17, 2016

Show MS CRM 2011/2013/2015 Optionset values in Dropdownlist in Asp.net

Few days back i have a very interesting requirement from one of my customer to show OptionSet values in dropdownlist in asp.net.

So we can use below code to populate optionset values in dropdownlist in asp.net Web application.

public Dictionary<int, string>

RetrieveOptionsetMetadata(IOrganizationService _iOgranizationService)
{
  
//Dictionary to store value and text

 Dictionary<int,string> _DropdownDatasource=newDictionary<int,string>();

 //Create request to fetch optionset

 RetrieveAttributeRequest _Request = new RetrieveAttributeRequest
{
EntityLogicalName ="contact",    // Name of Entity
LogicalName =“new_stream”,    // Name of Option Set field
RetrieveAsIfPublished =true
 };

 // Execute the request

 RetrieveAttributeResponse _Response =(RetrieveAttributeResponse)_iOgranizationService.Execute(_Request);
 PicklistAttributeMetadata _PicklistAttributeMetadata = (PicklistAttributeMetadata)_Response.AttributeMetadata;
 OptionMetadata[] optionList =_PicklistAttributeMetadata.OptionSet.Options.ToArray();
 foreach (OptionMetadata _Optionset in optionList) {
_DropdownDatasource.Add(int.Parse(_Optionset.Value.ToString()), _Optionset.Label.UserLocalizedLabel.Label);
}
 return _DropdownDatasource;
}
After that we can set datasource for dropdownlist like below
DropDownList1.DataSource = RetrieveOptionsetMetadata(_iOgranizationService);
DropDownList1.DataBind();


In ASP.Net it will look like below:

No comments:

Post a Comment

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...