Get OptionSet Label Using StringMap Web API

This article is about getting OptionSet label using Dynamics 365 StringMap Web API.

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.
  1. function GetOptionSetLable(entityname, attributename, attributevalue) {  
  2.     //prepare query options  
  3.     var query = "/api/data/v8.2/stringmaps?$filter=objecttypecode eq '" + entity + "' and  attributename eq '" + attributename + "' and attributevalue eq " + attributevalue;  
  4.     //setup webapi request  
  5.     var req = new XMLHttpRequest();  
  6.     req.open("GET", Xrm.Page.context.getClientUrl() + query, false);  
  7.     req.setRequestHeader("OData-MaxVersion""4.0");  
  8.     req.setRequestHeader("OData-Version""4.0");  
  9.     req.setRequestHeader("Accept""application/json");  
  10.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  11.     req.setRequestHeader("Prefer""odata.include-annotations=\"*\"");  
  12.     req.onreadystatechange = function () {  
  13.         if (this.readyState === 4) {  
  14.             req.onreadystatechange = null;  
  15.             if (this.status === 200) {  
  16.                 var results = JSON.parse(this.response);  
  17.                 if (results != null && results.value.length > 0)  
  18.                     Xrm.Utility.alertDialog(results.value[0].value);  
  19.    
  20.             } else {  
  21.                 Xrm.Utility.alertDialog(this.statusText);  
  22.             }  
  23.         }  
  24.     };  
  25.     req.send();  
  26. }  

I hope it will help someone!