Hello, I am trying to add a new feature layer to an existing feature layer collection on AGO via the add_to_definition operation using arcgis api for python (adding Ropes feature layer to Breckenridge_resort_SDM feature layer collection). I'm having trouble finding good code examples for adding a feature layer in this way using a JSON object (formatting). Here is my code as well as the error I'm getting. The layer I am trying to add to the feature layer collection is a layer within another hosted feature layer collection on AGO. I got the JSON object from copy and pasting the layer information from querying the properties of the feature layer collection. I would be so grateful to get some help on where I'm going wrong. Thanks
true should be True and null should be None in your dictionary.
You will also need to define the fields which you can get from your other layer, and you can also set the symbology, again you can get all this from the other layer. The dictionary will look something like the below. I found that by not setting an extent, the layer opens and zooms to Null Island.
You need to get the properties of the Feature Layer object to access the fields, symbology etc.
new_layer = {
"id" : 1, # will increment by default
"type" : "Feature Layer",
"name" : "NAME_OF_LAYER",
"description": "Layer Description",
"geometryType": "esriGeometryPolyline", # create a line layer
"extent": {
"xmin": -11.0,
"ymin": -52.0,
"xmax": -5.0,
"ymax": 56.0,
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
}
},
"fields" : [
{
"name": "FID", ## you could call this OBJECTID
"type": "esriFieldTypeOID",
"actualType": "int",
"alias": "FID",
"sqlType": "sqlTypeInteger",
"nullable": False,
"editable": False,
},
{
"name": "route_name",
"type": "esriFieldTypeString",
"actualType": "nvarchar",
"alias": "String Field",
"sqlType": "sqlTypeNVarchar",
"length": 25,
"nullable": True,
"editable": True
},
{
"name": "diff_rating",
"type": "esriFieldTypeSmallInteger",
"actualType": "int",
"alias": "Integer Field",
"sqlType": "sqlTypeInteger",
"nullable": True,
"editable": True
},
{
"name": "highest_m",
"type": "esriFieldTypeDouble",
"actualType": "float",
"alias": "Decimal Field",
"sqlType": "sqlTypeFloat",
"nullable": True,
"editable": True
}
],
"indexes": [
{
"name": "PK_IDX", # requires a primary key field
"fields": "FID",
"isAscending": True,
"isUnique": True,
"description": "clustered, unique, primary key"
}
],
"drawingInfo": {
"renderer": {
"type": "simple",
"symbol": {
"type": "esriSLS",
"style": "esriSLSSolid",
"color": [
165,
83,
183,
255
],
"width": 1
}
},
"transparency": 0
},
"objectIdField": "FID", # set the objectId field
"uniqueIdField": {
"name": "FID",
"isSystemMaintained": True
}
}
@Clubdebambos Thank you for your reply! I retrieved the information for my json object by accessing layer.properties for the layer I want to add (Ropes) to the feature layer collection (breck_SDM_test). I copied and pasted certain sections of the layer properties to fit the formatting in the code snippet you provided. I noticed some discrepancies, such as, the fields do not have "actualType" as one of the properties. Is this ok, or do I need to add this? Regardless, I think my json is good now. I ran the code and this is the error I'm getting.
I'm wondering whats wrong with my code to give this error. Does it matter if I use gis.content.search() vs gis.content.get() to retrieve the portal item the add_to_definition operator is applied to?
An item object has no manager, it is a FeatureLayerCollection object that you want to add the new layer to.
# add to top of the script
from arcgis.features import FeatureLayerCollection
flc = FeatureLayerCollection.fromitem(item)
# update the JSON definition fof the feature service to include the layer
flc.manager.add_to_definition({"layers": [new_layer]})
@Clubdebambos that helped a lot. I'm still having trouble updating the feature layer collection with the new layer on AGO. After running the script, the feature layer collection shows the new layer was added to the properties but this is not reflected on AGO when I go to the view details page. how do I update the featuren layer collection so I can view/edit on AGO? Here is my updated script and proof that the layer was added in the properties.
Upper case True on line 29
defaultVisibility : True