<?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: Adding multiple fields to hosted feature layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592675#M11207</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/9525"&gt;@JustinWolff&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I was able to reproduce your error and fix it. Add in the full field definitions as per below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayer

username = ''
password = ''

gis = GIS("&amp;lt;url&amp;gt;", username, password, verify_cert=False)

item_id = "&amp;lt;item_id&amp;gt;"

feature_layer_item = gis.content.get(item_id)

feature_layer = FeatureLayer.fromitem(feature_layer_item)

fields = feature_layer.properties.fields

existing_field_names = [field.name for field in fields]

## DEFINE THE FIELDS
new_fields = [
    {
        "name": "newfield1",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 1",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    },
    {
        "name": "newfield2",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 2",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    },
    {
        "name": "newfield3",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 3",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    }
]

## REMOVE ANY FIELDS FROM THE LIST THAT ARE ALREADY IN THE FEATURE LAYER
new_fields = [field_info for field_info in new_fields if field_info["name"] not in existing_field_names]

## ADD THE NEW FIELDS
feature_layer.manager.add_to_definition({"fields":new_fields})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps.&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Glen&lt;/P&gt;</description>
    <pubDate>Thu, 06 Mar 2025 08:44:51 GMT</pubDate>
    <dc:creator>Clubdebambos</dc:creator>
    <dc:date>2025-03-06T08:44:51Z</dc:date>
    <item>
      <title>Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592593#M11203</link>
      <description>&lt;P&gt;I'm attempting to add new fields to a hosted feature layer if they don't already exist.&amp;nbsp; Currently my code will add the first field in the list (dictionary), but then fails with the message:&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;Exception: Unable to add feature service layer definition.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Invalid definition for System.Collections.Generic.List`1[ESRI.ArcGIS.SDS.FieldInfo]&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Object reference not set to an instance of an object.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(Error Code: 400)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;If it's run a second time it does not add the second field and gives the same error.&amp;nbsp; I added the sleep thinking it may help but it doesn't.&amp;nbsp; Any suggestions appreciated.&amp;nbsp; Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import time

username = ''
password = ''

gis = GIS("&amp;lt;url&amp;gt;", username, password, verify_cert=False)

item_id = "&amp;lt;item_id&amp;gt;"

feature_layer_item = gis.content.get(item_id)

feature_layer = FeatureLayer.fromitem(feature_layer_item)
#feature_layer = FeatureLayer.fromitem(feature_url)

fields = feature_layer.properties.fields

existing_field_names = [field.name for field in fields]

new_fields = [
    {"name":"newfield1", "alias": "New Field 1","type": "esriFieldTypeInteger"},
    {"name":"newfield2", "alias": "New Field 2","type":"esriFieldtypeInteger"},
    {"name":"newfield3", "alias": "New Field 3","type":"esriFieldtypeInteger"}
    ]

fields_to_add_list = [] # this is named 'list' but is actually a dictionary
for new_field in new_fields:
    if new_field["name"] not in existing_field_names:
        fields_to_add_list.append(new_field)
        print(f"Field '{new_field['name']}' will be added.")
    else:
        print(f"Field '{new_field['name']}' already exists.")

for field_to_add in fields_to_add_list:
    print(field_to_add)
    feature_layer.manager.add_to_definition({"fields":[field_to_add]})
    time.sleep(20)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 00:26:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592593#M11203</guid>
      <dc:creator>JustinWolff</dc:creator>
      <dc:date>2025-03-06T00:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592604#M11204</link>
      <description>&lt;P&gt;Instead of running multiple calls to &lt;FONT face="courier new,courier"&gt;add_to_definition&lt;/FONT&gt; does doing every field in the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;fields_to_add_list&lt;/FONT&gt; list work better? I.e.:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;feature_layer.manager.add_to_definition({"fields": fields_to_add_list})&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 06 Mar 2025 00:51:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592604#M11204</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2025-03-06T00:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592621#M11205</link>
      <description>&lt;P&gt;That gives the same error, except it doesn't add the first field, when left out of brackets:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;feature_layer.manager.add_to_definition({"fields":fields_to_add_list})&lt;/LI-CODE&gt;&lt;P&gt;And does not create the first field when placed in brackets:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;feature_layer.manager.add_to_definition({"fields":[fields_to_add_list]})&lt;/LI-CODE&gt;&lt;P&gt;but gives a bit different error:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Exception: Unable to add feature service layer definition.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Invalid definition for System.Collections.Generic.List`1[ESRI.ArcGIS.SDS.FieldInfo]&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Unable to cast object of type 'System.Object[]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;(Error Code: 400)&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 03:48:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592621#M11205</guid>
      <dc:creator>JustinWolff</dc:creator>
      <dc:date>2025-03-06T03:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592622#M11206</link>
      <description>&lt;P&gt;and if I use field_to_add outside of any brackets it completes without any errors but doesn't add any fields&lt;/P&gt;&lt;LI-CODE lang="c"&gt;feature_layer.manager.add_to_definition({"fields":field_to_add})&lt;/LI-CODE&gt;&lt;P&gt;with the print statements as they currently are:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Field 'newfield1' will be added.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Field 'newfield2' will be added.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Field 'newfield3' will be added.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;[{'name': 'newfield1', 'alias': 'New Field 1', 'type': 'esriFieldTypeInteger'}, {'name': 'newfield2', 'alias': 'New Field 2', 'type': 'esriFieldtypeInteger'}, {'name': 'newfield3', 'alias': 'New Field 3', 'type': 'esriFieldtypeInteger'}]&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;{'name': 'newfield1', 'alias': 'New Field 1', 'type': 'esriFieldTypeInteger'}&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;{'name': 'newfield2', 'alias': 'New Field 2', 'type': 'esriFieldtypeInteger'}&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;{'name': 'newfield3', 'alias': 'New Field 3', 'type': 'esriFieldtypeInteger'}&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Am I missing something in the field definition?&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 03:57:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592622#M11206</guid>
      <dc:creator>JustinWolff</dc:creator>
      <dc:date>2025-03-06T03:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592675#M11207</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/9525"&gt;@JustinWolff&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I was able to reproduce your error and fix it. Add in the full field definitions as per below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.features import FeatureLayer

username = ''
password = ''

gis = GIS("&amp;lt;url&amp;gt;", username, password, verify_cert=False)

item_id = "&amp;lt;item_id&amp;gt;"

feature_layer_item = gis.content.get(item_id)

feature_layer = FeatureLayer.fromitem(feature_layer_item)

fields = feature_layer.properties.fields

existing_field_names = [field.name for field in fields]

## DEFINE THE FIELDS
new_fields = [
    {
        "name": "newfield1",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 1",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    },
    {
        "name": "newfield2",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 2",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    },
    {
        "name": "newfield3",
        "type": "esriFieldTypeInteger",
        "actualType": "int",
        "alias": "New Field 3",
        "sqlType": "sqlTypeInteger",
        "nullable": True,
        "editable": True
    }
]

## REMOVE ANY FIELDS FROM THE LIST THAT ARE ALREADY IN THE FEATURE LAYER
new_fields = [field_info for field_info in new_fields if field_info["name"] not in existing_field_names]

## ADD THE NEW FIELDS
feature_layer.manager.add_to_definition({"fields":new_fields})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps.&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Glen&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 08:44:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592675#M11207</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-03-06T08:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592713#M11208</link>
      <description>&lt;P&gt;Thanks Glen, that works well.&amp;nbsp; As a follow-on, my test data only has one layer [0].&amp;nbsp; I'll have other situations where there are multiple sublayers.&amp;nbsp; I'm a bit confused how to use gis.content to return the separate layers.&amp;nbsp; Any help pointing me in the right direction is appreciated.&amp;nbsp; Thanks again.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 13:27:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592713#M11208</guid>
      <dc:creator>JustinWolff</dc:creator>
      <dc:date>2025-03-06T13:27:07Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple fields to hosted feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592735#M11210</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/9525"&gt;@JustinWolff&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;The below will help. A Feature Service Item (FeatureLayerCollection) has a property called layers that returns a list of FeatureLayer objects for the service. You can iterate over these or access via indexing.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS

## acess ArcGIS Online
agol = GIS("home")

## get the Feature Service as an Item object
item = agol.content.get("FS_ITEM_ID")

## print all feature layer
## note: returns a list of FeatureLayer objects
print(item.layers)

## get the index and name for each layer
for index, fl in enumerate(item.layers):
    print(index, fl.properties.name)

## access via index
fl = item.layers[1]
print(fl)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 14:38:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/adding-multiple-fields-to-hosted-feature-layer/m-p/1592735#M11210</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-03-06T14:38:40Z</dc:date>
    </item>
  </channel>
</rss>

