What Esri API exist to wrap calls to MapServer/layers?

4622
5
05-08-2015 12:17 PM
ericliprandi
New Contributor III

Does Esri provide a JavaScript API to access information at the following URL:

https://<server>/arcgis/rest/services/<service>/MapServer/layers

We can obviously access it via a fairly plain HTTP call, but it seems that most of the Esri jsapi call handle authentication and error handling already. We would like to avoid rebuilding that if I don't have to.

We looked at the documentation for ArcGISDynamicMapServiceLayer as that is what we use for our data layer in the map, but it seems that LayerInfo does not contain a lot of information (it looks more like the straight call to /MapServer)

What we are really after for each layer is:

  • Field aliases
  • Subtype field information
  • Domains

This information is fully available on the /MapServer/layers call.

Tags (2)
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Eric,

  Here is a sample that uses esriRequest to get the info you are asking for:

<!DOCTYPE html>
<html>
<head>
  <title>Get ArcGIS Server Map Service Layers</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet" href="http://js.arcgis.com/3.13/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="http://js.arcgis.com/3.13/"></script>
  <script>
    require(["dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json", "dojo/_base/array", "dojo/string", "esri/request", "dojo/_base/lang", "dojo/domReady!"], function(dom, on, domClass, dojoJson, array, dojoString, esriRequest, lang) {

        dom.byId("url").value = "http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Geology/MapServer/Layers";
        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, layerInfo, pad, retval;
          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 = "";

          if ( response.hasOwnProperty("layers") ) {
            layerInfo = array.map(response.layers, function(l) {
              var lDomains = [];
              retval = pad("Layer:", 8, " ", true) + l.name + "\n" + pad("", 104, "-", true) + "\n";
              retval += pad("  Type Field Name:", 20, " ", true) + pad(l.typeIdField, 25, " ", true) + "\n";
              fieldInfo = array.map(l.fields, lang.hitch(this, function(f) {
                if(f.domain){
                  lDomains.push(pad("  Name", 10, " ", true) + pad(f.domain.name, 25, " ", true) +
                                pad("Type", 8, " ", true) + pad(f.domain.type, 25, " ", true));
                }
                return pad("Field:", 8, " ", true) + pad(f.name, 25, " ", true) +
                  pad("Alias:", 8, " ", true) + pad(f.alias, 30, " ", true) +
                  pad("Type:", 8, " ", true) + pad(f.type, 25, " ", true);
              }));
              if (lDomains.length > 0){
                retval += pad("  Domains:", 12, " ", true) + "\n" + pad("  ", 102, "*", true) + "\n";
                retval += lDomains.join("\n") + "\n" + pad("  ", 102, "*", true) + "\n";
              }
              return retval += fieldInfo.join("\n") + "\n" + pad("", 104, "-", true) + "\n\n";
            });
            dom.byId("content").value = layerInfo.join("\n");
          }else{
            dom.byId("content").value = "No layers 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 layers 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>
  <p>
    <h2>Content</h2>
    <p>Check the console for the full response. Here we'll display Layer Name, Type Field Name, Field names, aliases and types:</p>
    <textarea id="content"></textarea>
  </p>
</body>
</html>
ericliprandi
New Contributor III

Robert,

Thanks for the info. What does the esriRequest have over other standard REST providers (like jQuery or angularjs)?

Is it specific to calling ArcGIS server endpoints? does it handle ArcGIS's responses (which are always 200 but may contain an error code)?

Regards

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Eric,

   See this link about esriRequest for an explanation:

Retrieve data from a web server | Guide | ArcGIS API for JavaScript

If you are going to be using the JS API then why not use methods and modules from the API before using other third party solutions.

0 Kudos
ericliprandi
New Contributor III

Robert,

Thanks for the info. FWIW, we actually consider the Esri JSAPI 3rd party... our app is primarily an AngularJS app hitting our services. So, we already use AngularJS' $http and $resource for all our calls. I was just wondering if esriRequest did anything more that would make it a clear winner to call Esri services.

We'll use it for our couple of Esri calls for now.

Thanks for your help,

Eric.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Eric,

Glad to help. Now it is your turn to help the community by marking this question as answered. All you have to do is click the "Correct Answer" link (the one with the little green star) on the post that provided the answer for you. If the answer was not provided by one of the responders then you can mark any of the replies that you received as helpful by clicking on the "Actions" menu and choosing "Mark as Helpful"

0 Kudos