Get Optionset Label using stringmap- Web API

Requirement: Get Optionset label based on optionset value using client side code.
Solution: We have different option to implement this requirement. Earlier we wrote a post to get option set label using formatted values, today we are going to discuss how we can use stringmap to get optionset label using Web API. Stringmap entity store details about optionsets, it has following attributes:
stringmap
Based on above attributes, to get optionset label based on the value we need to use three following attributes:
stringmap1

We can use following WebAPI request by passing above parameters:

function GetOptionSetLable(entityname, attributename, attributevalue) {
    //prepare query options
    var query = "/api/data/v8.2/stringmaps?$filter=objecttypecode eq '" + entityname + "' and  attributename eq '" + attributename + "' and attributevalue eq " + attributevalue;
    //setup webapi request
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + query, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                if (results != null && results.value.length > 0)
                    Xrm.Utility.alertDialog(results.value[0].value);

            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

Hope it will help someone !!

2 thoughts on “Get Optionset Label using stringmap- Web API

  1. Pingback: Get Option Set value based on label in Microsoft Flow | HIMBAP

  2. Khadim Ali

    Hi Mahendar, thank you for the wonderful hack. I call it hack as I was not able to find stringmaps entry in the service document.

    Can we use it safely, because, if it is still supported then I wonder why it is not available in the service document?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *