get coded value from layer in DynamicMapService Layer

4485
6
Jump to solution
10-18-2012 07:29 AM
deleted-user-ugCMpXci8bn5
New Contributor III
Hello, sorry if this was answered already, I didn't find an answer here, this is probably simple...

I would like to display the coded value of an attribute from one layer in  in a text box...I am using a query in a dynamic map service layer.
Currently I just get the numeric range..I can see the coded values are available in the map service. 

Any ideas? Thanks, Jason
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Frequent Contributor
Send a request to get the meta data for the service and create an object that you can use to map coded values to actual values. Here's an example I've posted in the past:

<!doctype html> <html lang="en">      <head>     <meta charset="utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"     />     <title>Get ArcGIS Server Map Service Layer Field Names</title>     <script type="text/javascript">       var dojoConfig = {         isDebug: true       };     </script>     <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1"></script>     <style type="text/css">       #content {         width: 800px;         height: 200px;         padding: 5px;         overflow: auto;         border: solid 2px #AAAAAA;         background-color: #FFFFFF;         font-family: monospace;         -moz-border-radius: 5px;         -webkit-border-radius: 5px;         -o-border-radius: 5px;         border-radius: 5px;         -moz-box-shadow: 0 0 0.5em black;         -webkit-box-shadow: 0 0 0.5em black;         -o-box-shadow: 0 0 0.5em black;         box-shadow: 0 0 0.5em black;       }       .failure {         color: red;       }       #status {         font-size: 12px;       }     </style>     <script type="text/javascript">       dojo.ready(function () {         dojo.byId("url").value = "http://gisdemo1.cdm.com/ArcGIS/rest/services/MALDEN_MA_MOBILE_RELATE_FS/FeatureServer/1";         dojo.byId("content").value = "";         dojo.connect(dojo.byId("go"), "onclick", getContent);       });        function getContent() {         var url;         dojo.byId("content").value = "";         dojo.removeClass(dojo.byId("content"), "failure");         dojo.byId("status").innerHTML = "Downloading...";         url = dojo.byId("url").value;         if (url.length == 0) {           alert("Please enter a URL.");           return;         }         var requestHandle = esri.request({           "url": url,           "content": {             "f": "json"           },           "callbackParamName": "callback",         });         requestHandle.then(requestSucceeded, requestFailed);       }       function requestSucceeded(response, io) {         var fieldInfo, pad;         pad = dojo.string.pad;         console.log("Succeeded: ", response);         var fieldsWithDomains = {};         // loop throught the fields         dojo.forEach(response.fields, function(field) {           if ( field.domain ) {             // use the field name as a key             fieldsWithDomains[field.name] = field.domain;           }         });         var info = "";         // print out domain info         for ( field in fieldsWithDomains ) {           if ( fieldsWithDomains[field].codedValues ) {             info += field + " has a CV domain, codes/values: \n";             dojo.forEach(fieldsWithDomains[field].codedValues, function(cv) {               info += "\t" + pad(cv.code + ":  ", 4, " ", true) + " " + cv.name + "\n";             });           }         }         console.log("info: ", info);         dojo.byId("content").value = info;       }                     function requestFailed(error, io) {                  console.log("Failed: ", error);                  dojo.addClass(dojo.byId("content"), "failure");                  dojo.toJsonIndentStr = "  ";                  dojo.byId("content").value = dojo.toJson(error, true);                    }     </script>   </head>      <body style="font-family: Arial Unicode MS,Arial,sans-serif;">     <p>Use esri.request to download info coded value domains.</p>     <p>Enter the URL for a layer with fields that have coded values domains:<br>       <input type="text" id="url" size="75" />       <input id="go" type="button" value="GO" />       <br />Check the console for the full response.       <span id="status"></span>     </p>     <p>       <p>Fields with coded value domains:</p>       <textarea id="content"></textarea>     </p>   </body>  </html>

View solution in original post

0 Kudos
6 Replies
__Rich_
Occasional Contributor III
I'm having trouble working out the crux of your problem, could you try restating the question?  (maybe with an example)
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
Ok.

Here is the field as given in the map service:
(Type: esriFieldTypeSmallInteger, Alias: Program type, Domain: Coded Values: [1: General academic], [2: Alternative], [3: Special education], ...6 more... )

In the .mxd, the coded value (string) is given in the attributes when the feature is identified, but in my Javascript API web app, only the range (integer) value is output to my textbox (a content pane)...I would like instead to output the coded value (the string, 'general academic', etc...) to the textbox. 

(Actually,I also noticed that this same field is 'undefined' in my tabbed infoWindow, neither the integer or the coded value are output..)

Does that help explain the problem?

Thanks, Jason

(some code below):
query:
        
        orgQuery = new esri.tasks.Query();
        orgTask = new esri.tasks.QueryTask("http://" + mapDomain + "/ArcGIS/rest/services/serverNameAndLayer");
        orgQuery.outFields = ["Name", "Program_type", "Enrollment"];
        orgQuery.returnGeometry = false;


'text box' (printed to content pane):
 
        var locList = list.features;
     var locContent;
     locContent += "<table border='1'><tr><th>Name</th><th>Type</th><th>Enrollment</th></tr>";
     for (var i = 0, l = locList.length; i < l; i++)
     {
         locContent += "<tr><td>" + list.features.attributes['Name'] + "</td>";
            locContent += "<td>" + list.features.attributes['Program_type'] + "</td>";
            locContent += "<td>" + list.features.attributes['Enrollment'] + "</td>"; 
  
     }
     
     locContent += "</tr></table>";
     dojo.byId("schoolNameResults").innerHTML = locContent;
0 Kudos
derekswingley1
Frequent Contributor
Send a request to get the meta data for the service and create an object that you can use to map coded values to actual values. Here's an example I've posted in the past:

<!doctype html> <html lang="en">      <head>     <meta charset="utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"     />     <title>Get ArcGIS Server Map Service Layer Field Names</title>     <script type="text/javascript">       var dojoConfig = {         isDebug: true       };     </script>     <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1"></script>     <style type="text/css">       #content {         width: 800px;         height: 200px;         padding: 5px;         overflow: auto;         border: solid 2px #AAAAAA;         background-color: #FFFFFF;         font-family: monospace;         -moz-border-radius: 5px;         -webkit-border-radius: 5px;         -o-border-radius: 5px;         border-radius: 5px;         -moz-box-shadow: 0 0 0.5em black;         -webkit-box-shadow: 0 0 0.5em black;         -o-box-shadow: 0 0 0.5em black;         box-shadow: 0 0 0.5em black;       }       .failure {         color: red;       }       #status {         font-size: 12px;       }     </style>     <script type="text/javascript">       dojo.ready(function () {         dojo.byId("url").value = "http://gisdemo1.cdm.com/ArcGIS/rest/services/MALDEN_MA_MOBILE_RELATE_FS/FeatureServer/1";         dojo.byId("content").value = "";         dojo.connect(dojo.byId("go"), "onclick", getContent);       });        function getContent() {         var url;         dojo.byId("content").value = "";         dojo.removeClass(dojo.byId("content"), "failure");         dojo.byId("status").innerHTML = "Downloading...";         url = dojo.byId("url").value;         if (url.length == 0) {           alert("Please enter a URL.");           return;         }         var requestHandle = esri.request({           "url": url,           "content": {             "f": "json"           },           "callbackParamName": "callback",         });         requestHandle.then(requestSucceeded, requestFailed);       }       function requestSucceeded(response, io) {         var fieldInfo, pad;         pad = dojo.string.pad;         console.log("Succeeded: ", response);         var fieldsWithDomains = {};         // loop throught the fields         dojo.forEach(response.fields, function(field) {           if ( field.domain ) {             // use the field name as a key             fieldsWithDomains[field.name] = field.domain;           }         });         var info = "";         // print out domain info         for ( field in fieldsWithDomains ) {           if ( fieldsWithDomains[field].codedValues ) {             info += field + " has a CV domain, codes/values: \n";             dojo.forEach(fieldsWithDomains[field].codedValues, function(cv) {               info += "\t" + pad(cv.code + ":  ", 4, " ", true) + " " + cv.name + "\n";             });           }         }         console.log("info: ", info);         dojo.byId("content").value = info;       }                     function requestFailed(error, io) {                  console.log("Failed: ", error);                  dojo.addClass(dojo.byId("content"), "failure");                  dojo.toJsonIndentStr = "  ";                  dojo.byId("content").value = dojo.toJson(error, true);                    }     </script>   </head>      <body style="font-family: Arial Unicode MS,Arial,sans-serif;">     <p>Use esri.request to download info coded value domains.</p>     <p>Enter the URL for a layer with fields that have coded values domains:<br>       <input type="text" id="url" size="75" />       <input id="go" type="button" value="GO" />       <br />Check the console for the full response.       <span id="status"></span>     </p>     <p>       <p>Fields with coded value domains:</p>       <textarea id="content"></textarea>     </p>   </body>  </html>
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
great, thank you!

...any idea why I get 'undefined' as a result for the same field attribute in my infoWindow?
The infoWindow text layout structure is the same as the layout given above..I built the tabbed infoWindow around this example:

http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...
0 Kudos
derekswingley1
Frequent Contributor
great, thank you!

...any idea why I get 'undefined' as a result for the same field attribute in my infoWindow?
The infoWindow text layout structure is the same as the layout given above..I built the tabbed infoWindow around this example:

http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...


Not sure, can you start a new thread?

Also, that's a very outdated link...we really need to have those pages redirect to the current docs.
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
OK, i created a new thread here:
http://forums.arcgis.com/threads/69479-attribute-coded-value-shown-as-undefined-in-infoWindow?p=2424...

..and I used a more current link for that example (though the example is the same...)
0 Kudos