I am trying to use a Python script to update data in a hosted feature layer from a csv file. The new data (csv file) uploads to the Enterprise Portal 10.9.1 as expected. I can truncate the data in the hosted feature layer as well. However, when I try to append the new data to the existing hosted feature layer, I get the following error:
streetNamesData.append(item_id=newStreetsItem.id, upload_format="csv", source_info=para1, source_table_name=tableName, update_geometry=False) Exception: Error downloading item from Portal. (Error Code: 500)
Python script is written in ArcGIS Pro 3.0.6 Python environment. Below is my code.
import arcpy
import arcgis
from arcgis.gis import GIS
import os
import datetime as dt
# Variables
portalUrl = "thePortalUrl"
username = "myUserName" # Enterprise Username
password = "myPassword" # Enterprise Password
hostedStreetNamesItemId = "c1fb90623f47457ea0ee26a1256f2228" # Feature Service Item ID to update
# Local Feature Class name
runOnProd = True
sdeStreetNames = "KGIS.TRANS_STREETNAME"
# Environment Variables
arcpy.env.overwriteOutput = True
##############################################
#
# Get the current database SDE connection for test or prod
# based on the runOnProd flag
#
##############################################
def getSDEConnection():
if runOnProd:
return "prodConnection.sde"
else:
return "testConnection.sde"
if __name__ == "__main__":
# Create GIS object
print("Connecting to Enterprise")
gis = GIS(portalUrl, username, password)
# Create a filename with a timestamp for uniqueness
now_ts = str(int(dt.datetime.now().timestamp()))
tableName = "StreetNameExport_{}".format(now_ts)
filename = "{}\{}.csv".format(arcpy.env.scratchFolder, tableName)
csvTitle = "TEMP - {}".format(tableName)
# Get the street names table in SDE
sdeStreetNamesTable = "{}\\{}".format(getSDEConnection(), sdeStreetNames)
# Set the field mappings to only export the STREET_NAME field
fms = arcpy.FieldMappings()
fm_streetName = arcpy.FieldMap()
fm_streetName.addInputField(sdeStreetNamesTable, "STREET_NAME")
outFm_streetName = fm_streetName.outputField
outFm_streetName.name = "STREET_NAME"
fm_streetName.outputField = outFm_streetName
fms.addFieldMap(fm_streetName)
# Export the data to a csv file
arcpy.conversion.ExportTable(sdeStreetNamesTable, filename, "STREETNAME_STATUS = 1", "NOT_USE_ALIAS", fms)
newStreetsProperties={'title':csvTitle, 'description':'Temp CSV file for updating active street names' , 'tags':'temp'}
newStreetsItem = gis.content.add(item_properties=newStreetsProperties, data=filename)
# Get featureService/hostedTable layer to update
streetNamesLayer = gis.content.get(hostedStreetNamesItemId)
streetNamesData = streetNamesLayer.tables[0]
print("Truncating Existing Table")
print("Disabling Sync")
flc = arcgis.features.FeatureLayerCollection(streetNamesLayer.url, gis)
properties = flc.properties.capabilities
updateDict = {"capabilities": "Query", "syncEnabled": False}
flc.manager.update_definition(updateDict)
print("Truncating Feature Service")
streetNamesData.manager.truncate()
print("Enabling Sync")
updateDict = {"capabilities": properties, "syncEnabled": True}
flc.manager.update_definition(updateDict)
print("Appending features")
para1 = gis.content.analyze(item=newStreetsItem, file_type="csv")
streetNamesData.append(item_id=newStreetsItem.id, upload_format="csv", source_info=para1, source_table_name=tableName, update_geometry=False)
Thank you!!! I looked for something like that but didn't see it. Way better way to look at code.