Select to view content in your preferred language

How to add a new field to a Hosted Feature Layer?

3099
5
06-06-2020 12:54 PM
Labels (1)
Chris_Mahlke
Emerging Contributor

Hi, I am trying to add a new field to a Feature Layer (hosted).  I thought that I could call the Add Field function.

Am I not passing in the correct parameters?

 

Here is my Notebook link (below):

Notebook Preview

Code snippet:

# Item: Salzburg Austria Feature Layerinput_data = gis.content.get("e52a0f35741e4eb0b87f8f1f499f26be")# Item's layertype(input_data.layers[0])
try:    arcpy.AddField_management(in_table=input_data.layers[0],                                field_name="Description",                                field_type="TEXT",                                field_precision="",                                field_scale="",                                field_length="50000",                                field_alias="")except arcpy.ExecuteWarning:    print(arcpy.GetMessages(1))except arcpy.ExecuteError:    print(arcpy.GetMessages(2))else:    print("Success!")finally:    print("Complete")

I receive back the following error:

RuntimeError: Object: Error in executing tool

Any suggestions on what I am doing incorrectly?

arcgis online notebooks‌ arcgis notebooks beta

0 Kudos
5 Replies
Damian
by
Regular Contributor

I have exactly the same issue, and getting the same error message. My code snippit below. More or less the same as Chris's just organised a bit differently. Hoping someone knows the answer.

 

from arcgis.gis import GIS
from arcgis import features
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
arcpy.env.overwriteOutput = True
gis = GIS("https://ausbats.maps.arcgis.com", "<USERNAME>", "<PASSWORD>")
search_result = gis.content.search('title:Hipposideros_ater_X',item_type="Feature Layer")
bat_item = search_result[0]
bat_layers = bat_item.layers
fieldName = 'Taxon'
arcpy.AddField_management(bat_layers,fieldName,"TEXT",field_length=30)

 

I'm  using Notebooks in ArcGIS Pro

Did you end up finding a solution @Chris_Mahlke?

Regards, Damian

0 Kudos
Damian
by
Regular Contributor

Ok, I finally found a solution. I suspect you can't add a field to an AGOL feature layer using arcpy (I lost count of how many different code variations I tried). I eventually stumbled across this github link that provided the answer I was after. My code snippit below.

D

from arcgis.gis import GIS
from arcgis import features
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
arcpy.env.overwriteOutput = True
gis = GIS("https://ausbats.maps.arcgis.com", "<USERNAME>", "<PASSWORD>") 
bat_url = "https://services5.arcgis.com/wkEdAXzuNvKdAtLV/arcgis/rest/services/Hipposideros_ater_X/FeatureServer/0"
bat_fl = FeatureLayer(bat_url,gis)
new_field = {
    "name": "Taxon",
    "type": "esriFieldTypeString",
    "alias": "Taxon",
    "length": 30,
    "nullable": True,
    "editable": True,
    "visible": True,
    "domain": None
}
update_dict = {"fields": [new_field]}
bat_fl.manager.add_to_definition(update_dict)

 

benough
New Contributor

Thank you for this.

It helped me solve this excat same problem, but I just used the arcpy.AddField_management method, but using the URL of the Hosted Feature Layer as the 'in_table' parameter. Works a treat.

 

jcarlson
MVP Esteemed Contributor

Using a feature layer manager via Python, as @Damian shows, is certainly one way to do it, and a method that I use often myself. However, there's no limitation on the arcpy.management.AddField() function that would prevent you from using it against a hosted feature layer. I've tested it, and it works just fine.

This issue, I think, stems from attempting to use the function from a notebook. Using a standard notebook in AGOL, try running any arcpy function and you'll get an error, because arcpy is not an available module in the standard AGOL Notebook environment.

I tried opening your notebook link, but it doesn't show anything. Can you confirm which type of notebook you've got? You can see in the AGOL docs that ArcPy is available, but only by using the advanced notebook runtimes. That would be my first guess.

Also, if the notebook still exists, make sure it's public so that others can see the preview page.

- Josh Carlson
Kendall County GIS
0 Kudos
Damian
by
Regular Contributor

Hi Josh, thanks for your response.

As mentioned in my previous post, I'm using Notebook in ArcGIS Pro. So ArcPy is definitely available. I assume that your query about the notebook link is directed towards @Chris_Mahlke as I didn't post a link. Would you mind posting an example of how you have used arcpy.management.AddField() please @jcarlson ?

Regards, Damian

 

0 Kudos