<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Append data to Hosted feature layer in ArcGIS Enterprise Portal Questions</title>
    <link>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575560#M16125</link>
    <description>&lt;P&gt;Thank you!!!&amp;nbsp; I looked for something like that but didn't see it. Way better way to look at code.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2025 19:43:12 GMT</pubDate>
    <dc:creator>BryanLynn1</dc:creator>
    <dc:date>2025-01-14T19:43:12Z</dc:date>
    <item>
      <title>Append data to Hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575544#M16123</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;streetNamesData.append(item_id=newStreetsItem.id, upload_format="csv", source_info=para1, source_table_name=tableName, update_geometry=False) &lt;SPAN&gt;Exception: Error downloading item from Portal. &lt;SPAN&gt;(Error Code: 500)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Python script is written in ArcGIS Pro 3.0.6 Python environment. Below is my code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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) &lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 Jan 2025 19:45:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575544#M16123</guid>
      <dc:creator>BryanLynn1</dc:creator>
      <dc:date>2025-01-14T19:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Append data to Hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575556#M16124</link>
      <description>&lt;P&gt;please format the code&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 19:33:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575556#M16124</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2025-01-14T19:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Append data to Hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575560#M16125</link>
      <description>&lt;P&gt;Thank you!!!&amp;nbsp; I looked for something like that but didn't see it. Way better way to look at code.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 19:43:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-portal-questions/append-data-to-hosted-feature-layer/m-p/1575560#M16125</guid>
      <dc:creator>BryanLynn1</dc:creator>
      <dc:date>2025-01-14T19:43:12Z</dc:date>
    </item>
  </channel>
</rss>

