{"id":752,"date":"2012-06-19T16:07:15","date_gmt":"2012-06-19T16:07:15","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=752"},"modified":"2012-06-19T16:07:15","modified_gmt":"2012-06-19T16:07:15","slug":"get-optionset-label-based-on-optionset-value-using-javascript","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=752","title":{"rendered":"Get Optionset Label based on Optionset value using Javascript"},"content":{"rendered":"<p>I got one requirement to fetch optionset lable based on it&#8217;s value, then\u00a0I found this <a href=\"http:\/\/mileyja.blogspot.in\/2011\/07\/retrieve-optionset-in-jscript-or-net-in.html\">post <\/a>\u00a0which helped me to write required script.\u00a0We can write a Soap request to fetch optionset metadata based on it&#8217;s metadata id (Guid for global optionset, you can capture global optionset guid from URL\u00a0through opening global optionset record). once you have guid you can write a Soap request like below, make sure to change metadataid and optionsetname<\/p>\n<p>\u00a0function GetOptionsetLable(_Value) {<br \/>\n\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0 var _ServerURL = Xrm.Page.context.getServerUrl() + &#8220;\/XRMServices\/2011\/Organization.svc\/web&#8221;;<br \/>\n\u00a0\u00a0\u00a0 var MetadaID = &#8220;xxxx-xxx-xxx&#8230;&#8221;; \/\/Optionset guid<br \/>\n\u00a0\u00a0\u00a0 var _OptionLabel = null;<br \/>\n\u00a0\u00a0\u00a0 var xmlrequest = &#8220;&lt;s:Envelope xmlns:s=&#8221;<a href=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\/\">http:\/\/schemas.xmlsoap.org\/soap\/envelope\/<\/a>&#8220;&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;s:Body&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;Execute xmlns=&#8221;<a href=\"http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts\/Services\/\">http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts\/Services<\/a>&#8221; xmlns:i=&#8221;<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\/\">http:\/\/www.w3.org\/2001\/XMLSchema-instance<\/a>&#8220;&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;request i:type=&#8221;a:RetrieveOptionSetRequest&#8221; xmlns:a=&#8221;<a href=\"http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts\/\">http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts<\/a>&#8220;&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:Parameters xmlns:b=&#8221;<a href=\"http:\/\/schemas.datacontract.org\/2004\/07\/System.Collections.Generic\/\">http:\/\/schemas.datacontract.org\/2004\/07\/System.Collections.Generic<\/a>&#8220;&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:key&gt;MetadataId&lt;\/b:key&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:value i:type=&#8221;c:guid&#8221; xmlns:c=&#8221;<a href=\"http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/\/%22%3E%22+MetadaID+%22%3C\/b:value\">http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/&#8221;&gt;&#8221;+MetadaID+&#8221;&lt;\/b:value<\/a>&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;\/a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:key&gt;RetrieveAsIfPublished&lt;\/b:key&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:value i:type=&#8221;c:boolean&#8221; xmlns:c=&#8221;<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema\/%22%3Efalse%3C\/b:value\">http:\/\/www.w3.org\/2001\/XMLSchema&#8221;&gt;false&lt;\/b:value<\/a>&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;\/a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:key&gt;Name&lt;\/b:key&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;b:value i:type=&#8221;c:string&#8221; xmlns:c=&#8221;<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema\/%22%3EOptionsetName%3C\/b:value\">http:\/\/www.w3.org\/2001\/XMLSchema&#8221;&gt;<strong>OptionsetName<\/strong>&lt;\/b:value<\/a>&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;\/a:KeyValuePairOfstringanyType&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;\/a:Parameters&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:RequestId i:nil=&#8221;true&#8221; \/&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;a:RequestName&gt;RetrieveOptionSet&lt;\/a:RequestName&gt;&#8221; +<br \/>\n\u00a0\u00a0\u00a0 &#8220;&lt;\/request&gt;&lt;\/Execute&gt;&lt;\/s:Body&gt;&lt;\/s:Envelope&gt;&#8221;;<br \/>\n\u00a0\u00a0\u00a0 var req = new XMLHttpRequest();<br \/>\n\u00a0\u00a0\u00a0 req.open(&#8220;POST&#8221;, _ServerURL, false)<br \/>\n\u00a0\u00a0\u00a0 req.setRequestHeader(&#8220;Accept&#8221;, &#8220;application\/xml, text\/xml, *\/*&#8221;);<br \/>\n\u00a0\u00a0\u00a0 req.setRequestHeader(&#8220;Content-Type&#8221;, &#8220;text\/xml; charset=utf-8&#8221;);<br \/>\n\u00a0\u00a0\u00a0 req.setRequestHeader(&#8220;SOAPAction&#8221;, &#8220;<a href=\"http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts\/Services\/IOrganizationService\/Execute\">http:\/\/schemas.microsoft.com\/xrm\/2011\/Contracts\/Services\/IOrganizationService\/Execute<\/a>&#8220;);<br \/>\n\u00a0\u00a0\u00a0 req.send(xmlrequest);<br \/>\n\u00a0\u00a0\u00a0 var resultXml = req.responseXML;<\/p>\n<p>\u00a0\u00a0\u00a0 var errorCount = resultXml.selectNodes(&#8216;\/\/error&#8217;).length;<br \/>\n\u00a0\u00a0\u00a0 if (errorCount != 0) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var msg = resultXml.selectSingleNode(&#8216;\/\/description&#8217;).nodeTypedValue;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 alert(msg);<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 else {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var oXmlDoc = new ActiveXObject(&#8220;Microsoft.XMLDOM&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 oXmlDoc.async = false;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 oXmlDoc.loadXML(resultXml.xml);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var results = oXmlDoc.getElementsByTagName(&#8220;ExecuteResponse\/ExecuteResult\/a:Results\/a:KeyValuePairOfstringanyType\/b:value\/c:Options\/c:OptionMetadata&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for (i = 0; i &lt; results.length; i++) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var _Name = results[i].selectSingleNode(&#8216;.\/c:Label\/a:LocalizedLabels\/a:LocalizedLabel\/a:Label&#8217;).nodeTypedValue;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var Optionvalue = results[i].selectSingleNode(&#8216;.\/c:Value&#8217;).nodeTypedValue;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (_Value == Optionvalue) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _OptionLabel = _Name;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return _OptionLabel;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 return _OptionLabel;<br \/>\n}<\/p>\n<p>Enjoy !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got one requirement to fetch optionset lable based on it&#8217;s value, then\u00a0I found this post \u00a0which helped me to write required script.\u00a0We can write a Soap request to fetch optionset metadata based on it&#8217;s metadata id (Guid for global optionset, you can capture global optionset guid from URL\u00a0through opening global optionset record). once you have guid you can write&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=752\">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":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/752"}],"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=752"}],"version-history":[{"count":0,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}