I am trying to download a hosted layer from our portal and save to a file geodatabase.

Following Using ArcPy to copy a portal feature service to a fgdb feature class:

I've tried:

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

url_gis = r'https://maps.something.se/arcgis/home/'
url_fl = r'https://maps.something.se/server/rest/services/Hosted/service_asfdasfadsfasafdsafdsafdsafsafas653757237adfagdagd.'

user = "username"
pwd = "password"

#fgdb = # path to file geodatabase
#fc = # name of feature class

gis = GIS(url_gis, user, pwd)
fl = FeatureLayer(url_fl)
fs = fl.query() #No errors until this line

builtins.Exception: A general error occurred: maximum recursion depth exceeded while calling a Python object

Am I on the right track with this or are there some easier way to download the data using ArcPy?

  • 2
    This isn't ArcPy, it's the ArcGIS API for Python. If there isn't a FeatureClassToFeatureClass equivalent, you need to go the old-fashioned route of a nested row writer within a row query.
    – Vince
    Feb 8, 2022 at 13:00

1 Answer 1


Using ArcPy

import arcpy

## url to the feature layer
fl = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/County_Boundaries/FeatureServer/0"

## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"

## path for saving output feature class
out_fc = "{0}\\exported_layer".format(gdb)

## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
arcpy.conversion.ExportFeatures(fl, out_fc)

Using ArcGIS API for Python to download entire feature service.

from arcgis.gis import GIS

## required inputs
export_title = "fs_export"
export_format = "File Geodatabase"
save_path = r"C:\path\to\folder"

## access agol
agol = GIS("home")

## access the feature service
item = agol.content.get("FS_ITEM_ID")

export = item.export(
    title = export_title,
    export_format = export_format

    save_path = save_path

Using ArcGIS API for Python to extract an individual layer.

from arcgis.gis import GIS

## required inputs
out_fc = "fs_export"
gdb = r"C:\Path\to\agol.gdb"

## access agol
agol = GIS("home")

## access the feature service
item = agol.content.get("FS_ITEM_ID")

## access feature layer of choice
fl = item.layers[0]

## use query to return a feature set object
export = fl.query()

## save the feature set as a feature class in a gdb
export.save(gdb, out_fc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.