Query to Get All Features in the ArcGIS API for JavaScript

When you’re querying a Feature Service in the ArcGIS API for JavaScript, there’s typically a maximum number of features (records) that will be returned from any query – usually set to 1000. If you want to get all the features in the service, you’ll need to make multiple queries to the service. The best way to do this is with a recursive function that checks if the exceededTransferLimit property is true, and if so will continue making additional queries until complete.

One way to write that recursive function is like this

  const _getAllFeaturesRecursive = (layer, featuresSoFar) => {

    return layer
      .queryFeatures({
        start: featuresSoFar.length,
        num: layer.capabilities.query.maxRecordCount,
        outFields: ['*']
      })
      .then((results) => {
      // If "exceededTransferLimit" is true, then make another request (call 
      //  this same function) with a new "start" position. If not, return 
      // with the concatenated results.
        if (
          results.exceededTransferLimit &&
          results.exceededTransferLimit === true
        ) {
          return _getAllFeaturesRecursive(layer, [
            ...featuresSoFar,
            ...results.features
          ]);
        } else {
          return Promise.resolve([...featuresSoFar, ...results.features]);
        }
      });
  };

Wrapping that code in a function to kick it off and running it yields this example:

See the Pen Query to Get ALL the Features - ArcGIS API for JavaScript v4 by Gavin Rehkemper (@gavinr) on CodePen.

Check out the full code here.

Update: My colleague Yann Cabon mentioned that using ES6 Generator Functions, if you have access to use them in your environment (caniuse), might be an even easier way to do this. I concur! Check out his Codepen example here.

See Also