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!
I've been working on this too. There is a real need for it since we can't see the future or anticipate all needs of a layer at initial creation. Even ESRI Solutions need extended and they seem to include even the kitchen sink. Here's the best post I've found that even has info about a github repo with samples and a bug that shows a workaround to getting these newly added items to show in portal after adding them through the REST API.
You can add new layers to a hosted feature layer using the "add to definition" function of the REST API and Python API.
I would definitely test this out on a layer you don't care about first though, especially if you're adding any kind of relationships, it's very easy to mess this up and can be difficult to fix.
Another option would be to export your existing HFL to a File GDB, add a new feature class to the GDB in Pro, then republish the modified File GDB as a new HFL. This option is definitely safer and easier but can be more work if you have web maps and apps hanging off your existing HFL.
@Katie_Clark , thank you so much! You saved me a lot of time. So far, this is the only workaround that has worked for me using the ArcGIS Python API 2.4.2.It worked both on the hosted features and the views.
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.
@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 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....
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!
How a regular user can do this via ArcGIS PRO (no restAPI handling)?
If you go through Portal/ArcGIS Online to the Item Overview page and scroll down to the "URL" link, it will open it up in the rest services directory and then you can click "Admin" in the upper right. This window has the token already applied. Use the links at the bottom to "addToDefinition". Also, if it says that the token is expired, refresh the Overview page and repeat those steps.
How can i get a token to access the rest api? Its says token required
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.