Add a layer to a hosted feature layer

11509
13
Jump to solution
08-30-2023 08:20 AM
Labels (3)
IB3
by
Regular Contributor

I am trying to add an extra layer to a hosted feature layer. I dont find any function inside AGOL to do it directly and I have tried some code in Notebook, but it doesnt seem to be possible. Any ideas how I can do this?

 

See screenshot 

 

Thanks!

13 Replies
DougGreen
Frequent Contributor

I just tested adding a layer to an existing Hosted Feature Layer through the REST endpoints today and IT IS NOW FIXED! I used the addToDefinition with a JSON containing one layer. After it completed, I refreshed the item's Overview page and the new layer showed up. This is really great. It means AGOL can really be used as an actual Geodatabase for small organizations now. Thank you ESRI!

Katie_Clark
MVP Alum

@DougGreen That's awesome that you say it's fixed....unfortunately, I didn't experience the same behavior when I was working on this yesterday.

I wanted to share what I ended up doing, after lots of trial and error, which seemed to work. Below is a snippet from the script for the tool I built.

try:
    #  Add layer definition from JSON file to the service definition
    result = fs.manager.add_to_definition(config)
    
if result.get("success"):
        arcpy.AddMessage("Layer successfully added.")

        #  Update the item metadata with the new layer so it appears in the AGOL UI
        item.update(data={"layers": [dict(layer.properties) for layer in fs.layers]})
    else:
        arcpy.AddWarning(f"Failed to add layer. Response: {result}")

 

 

I found that I needed to use item.update, specifically using a dictionary structure like that, to properly get the item metadata to update to match the service definition.

How did you handle your workflow, in which you say it updated the item details automatically?

A potentially important detail, I was working with a hosted feature layer that was published from ArcGIS Pro. I believe the metadata for these is structured slightly differently compared to hosted feature layers created in ArcGIS Online directly. Perhaps that was a factor....

Best,
Katie

If this answer helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!
BRENNEJM
Regular Contributor

@Katie_ClarkThanks! This is so useful to have this code snippet and it worked for me!

Just to fill in some missing bits for others, here's a full code snippet:

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

# Fill in your username and password
gis = GIS('https://www.arcgis.com','username','password')

# Fill in the hosted feature layer ID
item = gis.content.get('feature layer id')
fs = FeatureLayerCollection.fromitem(item)

# Update the item metadata so that all layers appear in the AGO UI
item.update(data={"layers": [dict(layer.properties) for layer in fs.layers]})
DougGreen
Frequent Contributor

Sorry for no response there. It's been long enough that I don't have all the details anymore. The next time I have to do this, I know I'll be coming back here though, since the working status of this seems to change frequently.