Select to view content in your preferred language

Output name oddity using FeatureClassToGeodatabase to copy hosted feature layers in AGO

639
3
09-01-2023 09:44 AM
twangtx
New Contributor II

Hello, 

So I'm working on a python script to back up hosted feature layers in AGO to a local file geodatabase.

I'm aware of this one on ESRI's knowledge-base, https://support.esri.com/en-us/knowledge-base/how-to-back-up-hosted-content-by-looping-through-and-d... but it's a little more broad than I'm really looking for. And I'm trying to develop something that's friendly for people who aren't as familiar with python (or scripting in general). Idea being that someone can just add a layer to the map and it'll get included in the back up and you don't have to worry about getting tags, categories, or ownership stuff set up. Best practice aside, that's that reality.

Here's the script I'm running and it works but I sometimes get an odd output for the feature class names like "GPLYR - [GlobalID looking string]" (see snip below the code).

# Set the Pro project (.aprx) that contains the layers you want to share/publish.
prj = arcpy.mp.ArcGISProject('Current') # Set, uses this aprx
mp = prj.listMaps("Storm Damage Assessment")[0] 
lyrlist = mp.listLayers()

# Set output locations and name
outdir = r"C:\GIS Data" # SET, where should the file geodatabase and feature classes go?
fgdbname = r"Storm Damage Assessment" # SET, name the new file geodatabase to be created.

# Date/time
date = time.strftime("%Y%m%d")

AGOBackupfgdb = arcpy.management.CreateFileGDB(outdir, fgdbname + "_" + date)
#AGOBackuppath = os.path.join(outdir, fgdbname + "_" + date + ".gdb")
#print(AGOBackuppath)

try:
      for lyr in lyrlist:
            print ("Exporting " + lyr.name)
            arcpy.conversion.FeatureClassToGeodatabase(lyr,AGOBackupfgdb)
             #arcpy.conversion.ExportFeatures(lyrL, AGOBackuppath + "\\" + lyr.name)
             print("\t Success")
except:
       print("Failed to export to file geodatabase.")

twangtx_0-1693585684304.png

I've also written another version of the script that loops through all the maps in the Pro Project to copy the hosted feature layers to file geodatabase (one per map). Usually one of the back up sets of hosted feature layers has the correct names but the others have that GPLYR name for the feature class.

try:
     for m in mp:
            print("Backing up layers from " + m.name)
            lyrlist = m.listLayers()
            AGOBackupfgdb = arcpy.management.CreateFileGDB(outdir, m.name + "_" + date)
            lyrlist = m.listLayers()
           for lyr in lyrlist:
                print ("\t" + lyr.name)
                arcpy.conversion.FeatureClassToGeodatabase(lyr,AGOBackupfgdb)
                print("\t Done \n")
                print("Successfully backed up layers")
except:
             print("Failed")

Maybe FeatureClasstoGeodatabase isn't the best method? I'm trying to use ExportFeatures, but for whatever reason I cannot get it to work. It looks like there are some parameters from FeatureClasstoFeatureClass that didn't get carried over to ExportFeatures when it get deprecated.

Thank you for any help or input on improving the script methodology or why some feature classes are getting named GPLYR and how to solve it.

Thomas

0 Kudos
3 Replies
RhettZufelt
MVP Notable Contributor

Not sure where that is coming from, but I use a modified version of the link you provided.

Once connected, you can query by Hosted Feature Service name rather than worrying about tags, ownership, etc.

query_string =  f"type:Feature Service, title:SSA_Map_Data"

But will backup the entire HFS, not just individual layers.

In case it helps,

R_

rfan27
by
New Contributor

Does anyone else get the error below when arcpy.conversion.ExportFeatures(x,y,"alias"...) is ran?
AttributeError: 'module' object has no attribute 'ExportFeatures'

0 Kudos
RhettZufelt
MVP Notable Contributor

If I try to run it in Pro 2.x I get that error as it doesn't appear as if it was added until Pro 3.

R_