Update arcgis python API to 2.4.0 last week, now I can't seem to make FeatureLayerCollection.manager.add_to_definition() work.
I have this script which ran perfectly fine before where I added a layer or table to a FeatureLayerCollection: flc.manager.add_to_definition(dict). Where dict contains info about the layer in question.
Now I get this error:
Exception: Invalid URL
Invalid URL
(Error Code: 400)
What's wrong here?
Thanks.
Solved! Go to Solution.
Well.... Found the problem, which was my fault 🙂
To populate the "sourceServiceName" in the "viewLayerDefinition", I was using the variable from the service Item service.title or service.name. But I didn't realize that this could be different from the FeatureLayerCollection name, which I can get from flc.manager.properties.adminServiceInfo.name.
The difference in my case was that there is no underscore in the service name and there is one in the flc name (for example: "this name" vs "this_name"). So the API couldn't find the sourceServiceName when trying to add the view.
To find my error, I created a view from the AGOL interface and tracked network calls in developer mode and compared the requests and responses to what I was sending.
Thanks for your help everybody! Stupid mistake 🙂
Hi @mikaël,
Are you able to share more of your code please?
How are you accessing ArcGIS Online (or Portal) and how are you creating the FeatureLayerCollection object.
All the best,
Glen
Hey,
Here goes (to create a view):
from arcgis.features import FeatureLayerCollection
from arcgis.gis import GIS
gis = GIS("URL", username, password)
service = gis.content.get("itemid")
flc = FeatureLayerCollection.fromitem(service)
dict = {
"layers": [
{
"adminLayerInfo": {
"viewLayerDefinition": {
"sourceServiceName": "nameOfSourceService"
"sourceLayerId": 0 # source service layer id
"sourceLayerFields": "*"
}
},
"id": 0,
"name": "name"
"description": "desc"
}
]
}
result = flc.manager.add_to_definition(dict)
# Exception: Invalid URL
# Invalid URL
# (Error Code: 400)
Hi @mikaël,
Are you trying to create a view? You will need to use the FeatureLayerCollection create_view() method.
You can do some upfront settings with the create_view() method, and you can manipulate the JSON of the view service with the FeatureLayerCollection add_to, update, and delete_from_definition().
You can also use the ViewManager for an Item object.
Right. I know I can use create_view() but I prefered using creating an empty service (create_service()) with the correct parameters/definitions and then adding the layers/tables and setting the view.
I'll try create_view() again to see if the problem occurs that way also.
You might want to post details here as an issue since it hasn't been reported and more information will be needed to test to see if it can be replicated
Esri/arcgis-python-api: Documentation and samples for ArcGIS API for Python
Well.... Found the problem, which was my fault 🙂
To populate the "sourceServiceName" in the "viewLayerDefinition", I was using the variable from the service Item service.title or service.name. But I didn't realize that this could be different from the FeatureLayerCollection name, which I can get from flc.manager.properties.adminServiceInfo.name.
The difference in my case was that there is no underscore in the service name and there is one in the flc name (for example: "this name" vs "this_name"). So the API couldn't find the sourceServiceName when trying to add the view.
To find my error, I created a view from the AGOL interface and tracked network calls in developer mode and compared the requests and responses to what I was sending.
Thanks for your help everybody! Stupid mistake 🙂