Append Hosted Layer to Feature Class

401
3
04-24-2023 06:31 AM
MattFancher1
New Contributor III

Hello all. I'm attempting a script to append records from an ArcGIS Online feature layer to a local geodatabase feature class.  I want to run the script nightly as a scheduled task. The below works well when I have Pro open and am logged into ArcGIS Online as my Portal. It fails when I run it as a stand alone script, saying the hosted feature layer does not exist or is not supported. Any advice how to accomplish the append properly? Thanks very much in advance.

import arcpy
from arcgis import GIS

#connect to ArcGIS Online
agol = GIS(username="username",password="password")

#find the hosted feature layer
item = agol.content.get("6ceffb124830415a9149e7c965e0fd12")
lyr = item.layers[0].url

#reference the geodatbase feature class
fc = r"C:\temp\gdb.sde\GISDB.GIS.TARGET_FC"

#empty the feature class
arcpy.management.TruncateTable(fc)

#append from the feature layer to the feature class
arcpy.Append_management(lyr, fc, "NO_TEST")

 

 

0 Kudos
3 Replies
RhettZufelt
MVP Frequent Contributor

You are not establishing the connection to your AGOL portal (when logged in in Pro, that is automagic).

Maybe something like (you supply username and password, but need to identify portal URL):

 

#connect to ArcGIS Online
agol = GIS(url='https://yourorg.maps.arcgis.com/',username="username",password="password")

 

R_

0 Kudos
MattFancher1
New Contributor III

Thank you @RhettZufelt  for the response! I tried your suggestion, but unfortunately got the same result saying the feature layer does not exist or is not supported. Any other ideas?

0 Kudos
RhettZufelt
MVP Frequent Contributor

Suspect maybe the way you are "getting" the feature layer isn't supported for the append tool.

When I append from HFS to local FGDB, I just use something like:

 

 

import arcpy
from arcgis.gis import GIS

arcpy.env.preserveGlobalIds = True

fc = r"C:\temp\gdb.sde\GISDB.GIS.TARGET_FC"


gis = GIS(url='https://yourorghere.maps.arcgis.com/', username='AdminUser', password='UsersPass')


in_lyr = 'https://services3.arcgis.com/5asfasga45zb/arcgis/rest/services/GMTest/FeatureServer/1'

#arcpy.management.TruncateTable(fc)

arcpy.Append_management(in_lyr, fc, "NO_TEST")

 

 

 

R_

0 Kudos