{"id":743,"date":"2012-06-10T14:44:42","date_gmt":"2012-06-10T14:44:42","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=743"},"modified":"2015-10-05T09:50:51","modified_gmt":"2015-10-05T09:50:51","slug":"show-ms-crm-2011-optionest-values-in-dropdownlist-in-asp-net","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=743","title":{"rendered":"Show MS CRM Optionset values in Dropdownlist in Asp.net"},"content":{"rendered":"<p>Sometime customers have requirement to create MS CRM records through custom asp.net pages, and if we have optionset in our entity we might want to display optionset valeus in dropdownlist in our Web page. We can use following code to populate\u00a0optionset values in dropdownlist.<\/p>\n<p>public\u00a0<span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">Dictionary<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">&lt;<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">int<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">, <\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">string<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">&gt; RetrieveOptionsetMetadata(<\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">IOrganizationService <\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">_iOgranizationService)<\/span><\/p>\n<p>{\u00a0\/<span style=\"font-family: Consolas; color: #008000; font-size: small;\">\/Dictionary to store value and text<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">Dictionary<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">&lt;<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">int<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">,<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">string<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">&gt; _DropdownDatasource=<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">new<\/span><\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">Dictionary<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">&lt;<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">int<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\">,<\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">string<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">&gt;();<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #008000; font-size: small;\">\/\/Create request to fetch optionset<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">RetrieveAttributeRequest<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\"> _Request = <\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">new\u00a0<\/span><\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">RetrieveAttributeRequest<\/span>{<\/p>\n<p>EntityLogicalName =<span style=\"font-family: Consolas; color: #a31515; font-size: small;\"><span style=\"font-family: Consolas; color: #a31515; font-size: small;\"><span style=\"font-family: Consolas; color: #a31515; font-size: small;\">&#8220;EntityName&#8221;<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">, \/\/Change entity name here<\/span><\/p>\n<p>LogicalName =<span style=\"font-family: Consolas; color: #a31515; font-size: small;\"><span style=\"font-family: Consolas; color: #a31515; font-size: small;\"><span style=\"font-family: Consolas; color: #a31515; font-size: small;\">&#8220;OptionsetFieldName&#8221;<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">, \/\/Change field name here<\/span><\/p>\n<p>RetrieveAsIfPublished =<span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">true<\/span><\/p>\n<p>};<\/p>\n<p><span style=\"font-family: Consolas; color: #008000; font-size: small;\">\/\/ Execute the request<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">RetrieveAttributeResponse<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\"> _Response =(<\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">RetrieveAttributeResponse<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">)_iOgranizationService.Execute(_Request);<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">PicklistAttributeMetadata<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\"> _PicklistAttributeMetadata = (<\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">PicklistAttributeMetadata<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">)_Response.AttributeMetadata;<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">OptionMetadata<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">[] optionList =_PicklistAttributeMetadata.OptionSet.Options.ToArray();<\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">foreach<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\"> (<\/span><\/span><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\"><span style=\"font-family: Consolas; color: #2b91af; font-size: small;\">OptionMetadata<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\"><span style=\"font-family: Consolas; font-size: small;\"> _Optionset <\/span><\/span><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">in <\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">optionList) {<\/span><\/p>\n<p>_DropdownDatasource.Add(<span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">int<\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">.Parse(_Optionset.Value.ToString()), _Optionset.Label.UserLocalizedLabel.Label);<\/span><\/p>\n<p>}<\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\"><span style=\"font-family: Consolas; color: #0000ff; font-size: small;\">return <\/span><\/span><\/span><span style=\"font-family: Consolas; font-size: small;\">_DropdownDatasource;<\/span><\/p>\n<p>}<\/p>\n<p>After that we can set datasource for dropdownlist like below<\/p>\n<p>DropDownList1.DataSource = RetrieveOptionsetMetadata(_iOgranizationService);<\/p>\n<p>DropDownList1.DataBind();<\/p>\n<p>Enjoy !!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometime customers have requirement to create MS CRM records through custom asp.net pages, and if we have optionset in our entity we might want to display optionset valeus in dropdownlist in our Web page. We can use following code to populate\u00a0optionset values in dropdownlist. public\u00a0Dictionary&lt;int, string&gt; RetrieveOptionsetMetadata(IOrganizationService _iOgranizationService) {\u00a0\/\/Dictionary to store value and text Dictionary&lt;int,string&gt; _DropdownDatasource=newDictionary&lt;int,string&gt;(); \/\/Create request to fetch&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=743\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,3,5],"tags":[],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/743"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":4,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions\/1691"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}