Hide Table of Contents
View Request layer info sample in sandbox
Request layer info

Description

esri/request can be used to get info about any object in the ArcGIS Server services directory. In this sample, esriRequest is used to get metadata about a layer in a map service. All of the info for the layer is printed to the console as a string and info about the layer's fields are printed to a textarea.

The technique used here could also be used to get info about a map service or any other service published to the services directory.

Code

<!DOCTYPE html>
<html>
<head>
  <title>Get ArcGIS Server Map Service Layer Field Names</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet" href="https://js.arcgis.com/3.29/esri/css/esri.css">
  <style>
    body{
      font-family: "Arial Unicode MS, Arial, sans-serif";
    }
    #content {
      width: 800px; height: 350px; padding: 5px; overflow: auto;
      border: solid 2px #AAAAAA; background-color: #FFFFFF;
      -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 src="https://js.arcgis.com/3.29/"></script>
  <script>
    require(["dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json", "dojo/_base/array", "dojo/string", "esri/request", "dojo/domReady!"], function(dom, on, domClass, dojoJson, array, dojoString, esriRequest) {

        dom.byId("url").value = "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/ArcGIS/rest/services/ZillowNeighborhoods-CA/FeatureServer/0";
        dom.byId("content").value = "";
        //handle the Go button's click event
        on(dom.byId("submitRequest"), "click", getContent);


        function getContent(){

          var contentDiv = dom.byId("content");
          contentDiv.value = "";
          domClass.remove(contentDiv, "failure");
          dom.byId("status").innerHTML = "Downloading...";

          //get the url and setup a proxy 
          var url = dom.byId("url").value;

          if(url.length === 0){
            alert("Please enter a URL");
            return;
          }

          var requestHandle = esriRequest({
            "url": url,
            "content": {
              "f": "json"
            },
            "callbackParamName": "callback"
          });
          requestHandle.then(requestSucceeded, requestFailed);
        }

        function requestSucceeded(response, io){
          var fieldInfo, pad;
          pad = dojoString.pad;

          //toJson converts the given JavaScript object
          //and its properties and values into simple text 
          dojoJson.toJsonIndentStr = "  ";
          console.log("response as text:\n", dojoJson.toJson(response, true));
          dom.byId("status").innerHTML = "";

          //show field names and aliases
          if ( response.hasOwnProperty("fields") ) {
            console.log("got some fields");
            fieldInfo = array.map(response.fields, function(f) {
              return pad("Field:", 8, " ", true) + pad(f.name, 25, " ", true) + 
                pad("Alias:", 8, " ", true) + pad(f.alias, 25, " ", true) + 
                pad("Type:", 8, " ", true) + pad(f.type, 25, " ", true);
            });
            dom.byId("content").value = fieldInfo.join("\n");
          } else {
            dom.byId("content").value = "No field info found. Please double-check the URL.";
          }

        }
        function requestFailed(error, io){

          domClass.add(dom.byId("content"), "failure");
          
          dojoJson.toJsonIndentStr = " ";
          dom.byId("content").value = dojoJson.toJson(error, true);

        }


    });
  </script>

</head>
<body>
  <p>Enter the URL for a layer in a map service to access metadata about a layer in a map service using esriRequest.</p>
  <p>
    <input type="text" id="url" size="105"/>
    <input id="submitRequest" type="button" value="GO" />
    <span id="status"></span>
  </p>
  <h2>Content</h2>
  <p>Check the console for the full response. Here we'll display Field names, aliases and types:</p>
  <textarea id="content"></textarea>
</body>
</html>
 
          
Show Modal