Get Optionset Label based on Optionset value using Javascript

I got one requirement to fetch optionset lable based on it’s value, then I found this post  which helped me to write required script. We can write a Soap request to fetch optionset metadata based on it’s metadata id (Guid for global optionset, you can capture global optionset guid from URL through opening global optionset record). once you have guid you can write a Soap request like below, make sure to change metadataid and optionsetname

 function GetOptionsetLable(_Value) {
    var _ServerURL = Xrm.Page.context.getServerUrl() + “/XRMServices/2011/Organization.svc/web”;
    var MetadaID = “xxxx-xxx-xxx…”; //Optionset guid
    var _OptionLabel = null;
    var xmlrequest = “<s:Envelope xmlns:s=\”\“>” +
    “<s:Body>” +
    “<Execute xmlns=\”\” xmlns:i=\”\“>” +
    “<request i:type=\”a:RetrieveOptionSetRequest\” xmlns:a=\”\“>” +
    “<a:Parameters xmlns:b=\”\“>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>MetadataId</b:key>” +
    “<b:value i:type=\”c:guid\” xmlns:c=\”\”>”+MetadaID+”</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>RetrieveAsIfPublished</b:key>” +
    “<b:value i:type=\”c:boolean\” xmlns:c=\”\”>false</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “<a:KeyValuePairOfstringanyType>” +
    “<b:key>Name</b:key>” +
    “<b:value i:type=\”c:string\” xmlns:c=\”\”>OptionsetName</b:value>” +
    “</a:KeyValuePairOfstringanyType>” +
    “</a:Parameters>” +
    “<a:RequestId i:nil=\”true\” />” +
    “<a:RequestName>RetrieveOptionSet</a:RequestName>” +
    var req = new XMLHttpRequest();“POST”, _ServerURL, false)
    req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
    req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
    req.setRequestHeader(“SOAPAction”, ““);
    var resultXml = req.responseXML;

    var errorCount = resultXml.selectNodes(‘//error’).length;
    if (errorCount != 0) {
        var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;
    else {
        var oXmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
        oXmlDoc.async = false;
        var results = oXmlDoc.getElementsByTagName(“ExecuteResponse/ExecuteResult/a:Results/a:KeyValuePairOfstringanyType/b:value/c:Options/c:OptionMetadata”);
        for (i = 0; i < results.length; i++) {
            var _Name = results[i].selectSingleNode(‘./c:Label/a:LocalizedLabels/a:LocalizedLabel/a:Label’).nodeTypedValue;
            var Optionvalue = results[i].selectSingleNode(‘./c:Value’).nodeTypedValue;
            if (_Value == Optionvalue) {
                _OptionLabel = _Name;
        return _OptionLabel;
    return _OptionLabel;

Enjoy !!


Leave a comment

Filed under MS CRM 2011

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s