Requirement
Get OptionSet label based on the OptionSet values usingclient-side code.
Solution
We have several options to implement this requirement. Earlier, we wrote a post to get OptionSet label using
formatted values. Today, we are going to discuss how we can use StringMap to get this OptionSet label using Web API. StringMap entity store gives details about optionsets. It has the following attributes.
Based on the above attributes, to get OptionSet label based on the values, we need to use the following three attributes.
We can use the following WebAPI request by passing the above parameters.
- function GetOptionSetLable(entityname, attributename, attributevalue) {
-
- var query = "/api/data/v8.2/stringmaps?$filter=objecttypecode eq '" + entity + "' and attributename eq '" + attributename + "' and attributevalue eq " + attributevalue;
-
- 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();
- }
I hope it will help someone!