How can I access to fields collection of some published layer?

1254
3
Jump to solution
08-25-2016 01:26 AM
MargaRaiz
New Contributor III

Hi everybody!

First of all, I'm a new WAB developer and I'm quite lost.. I'm trying to build a new custom widget and my first task is to load into a drop-down list the values of some specific field of the layer that is published in my webmap, but I am not sure how to access to fields collection, I have beed doing some research and I think that is throught "esri/layers/FeatureLayer" but I'm not sure. Can some one guide me a little bit please?

I have been able to acces to the layerinfo by "jimu/LayerInfos/LayerInfos" but I don't think that this library gives me access to fields collection of the layer.

Thanks you very much!

Marga

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Marga,

  There is a class in WAB that is designed for retrieving the layers used in the WAB map.

LayerInfos class—Web AppBuilder for ArcGIS (Developer Edition) | ArcGIS for Developers 

You would use code like this to loop through an array of operational layer until you find your specific layer and then use its fields property (if it is a FeatureLayer).

require(["jimu/LayerInfos/LayerInfos "], function(LayerInfos) {
 LayerInfos.getInstance(this.map, this.map.itemInfo).then(function(layerInfosObject){
layerInfosObject.getLayerInfoArray().forEach(function(layerInfo) {
       console.log(layerInfo.title, layerInfo.id);
     });
  });
});

or if it is just one specific layer that you need it may be easier to get the fields array using an esriRequest.

        esriRequest({
          url:"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M...",
          content:{
            f:'json'
          },
          handleAs:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(response){
          console.info(response.fields);
          alert("Data was retrieved");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Marga,

  There is a class in WAB that is designed for retrieving the layers used in the WAB map.

LayerInfos class—Web AppBuilder for ArcGIS (Developer Edition) | ArcGIS for Developers 

You would use code like this to loop through an array of operational layer until you find your specific layer and then use its fields property (if it is a FeatureLayer).

require(["jimu/LayerInfos/LayerInfos "], function(LayerInfos) {
 LayerInfos.getInstance(this.map, this.map.itemInfo).then(function(layerInfosObject){
layerInfosObject.getLayerInfoArray().forEach(function(layerInfo) {
       console.log(layerInfo.title, layerInfo.id);
     });
  });
});

or if it is just one specific layer that you need it may be easier to get the fields array using an esriRequest.

        esriRequest({
          url:"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M...",
          content:{
            f:'json'
          },
          handleAs:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(response){
          console.info(response.fields);
          alert("Data was retrieved");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
MargaRaiz
New Contributor III

Hi Robert!

Thank you very much for your answer, I have been able to retrieve the information using Esri Response and also from my layer because it has been published with Featureclass capabilitie.

               
        LayerInfos.getInstance(map, map.itemInfo).then(lang.hitch(function (operLayerInfos) {
        operLayerInfos.getLayerInfoArray().forEach(function (layerInfo) {
            var layerId = layerInfo.id;
            console.log('layer ID = ', layerId);
            var layer = map.getLayer(layerId);            
            console.log (layer.fields);
            console.log (layer.fields[0].name);
        }

Now, I have two other questions:

- Which methoth is better to retrieve the information if the layer is published with Featureclass capabilitie?

- I I want to get the uniques values of a field, do I need to loop throw all features of the layer?

Thanks!

Marga

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Marga,

  1. The first method is better as there is already an object populated with fields array in memory and there is no need to make an additional trip to the server.
  2. If you need unique values then you need to do an esriRequest or a QueryTask with a sql query that does a unique value return. This is not available on all ArcGIS servers versions though