0

I have been asked to create field map smartforms from the hosted feature layer & webmap.

Any example would be helpful!

My code seems to be not working, it does not create Form element.

from arcgis.gis import GIS
from arcgis.mapping import WebMap
from arcgis.features import FeatureLayer



_username= "<USERNAME>"
_password = "<PASSWORD>"
_url = "https://<test>.maps.arcgis.com/"   
gis = GIS(_url,_username,_password)

web_map_id = "790yym22618g2488a2b67adc300op38a" 
web_map_item = gis.content.get(web_map_id)
web_map = WebMap(web_map_item)


layer = web_map.definition["operationalLayers"][0]

feature_layer = FeatureLayer(layer['url'])

fields = feature_layer.properties.fields

# Filter out system fields like ObjectID, GlobalID, etc.
excluded_fields = ['objectid', 'globalid', 'created_user', 'created_date', 'last_edited_user', 'last_edited_date']
form_elements = []

for field in fields:
    field_name = field['name'].lower()
    if field_name in excluded_fields or field['type'] in ['esriFieldTypeOID', 'esriFieldTypeGlobalID']:
        continue
    
    form_elements.append({
        "type": "field",
        "fieldName": field['name'],
        "label": field.get('alias', field['name']),
        "required": not field.get('nullable', True),
        "visible": True
    })

# --- STEP 4: Construct the formInfo ---
form_info = {
    "expressionInfos": [],
    "lastEditInfos": {
        "authoringApp": "ArcGIS API for Python",
        "authoringAppVersion": "2.2.0",
        "editingTool": "Form"
    },
    "formElements": [
        {
            "type": "group",
            "label": "Auto-generated Form",
            "description": "Fields from Hosted Feature Layer",
            "visible": True,
            "formElements": form_elements
        }
    ],
    "version": 1
}

# --- STEP 5: Update the Web Map layer with formInfo ---
layer["popupInfo"] = {
    "title": "{%s}" % form_elements[0]["fieldName"],  # Use the first field as title
    "fieldInfos": [],
    "description": "",
    "expressionInfos": [],
    "formInfo": form_info
}

# --- STEP 6: Save the web map ---
web_map.update()
print("✅ FormInfo has been created and attached to the Web Map.")```

1 Answer 1

0

When manipulating the WebMap JSON you need to update the WebMap Ttem object and not the WebMap object (Map object from version 2.4.0).

web_map_item = gis.content.get(web_map_id)

## get the WebMap JSON definition as a dictionary
## same as WebMap.definition but this is removed in 2.4.0+
## so this way is future proffed
web_map_item_data = web_map_item.get_data()

## get the layer definition
layer = web_map_item_data["operationalLayers"][0]

"""
    create the Form JSON (dict)
    form_info = {your_definition}
"""

## apply the form definition to the layer
layer["formInfo"] = form_info

## update the layer in the overall WebMap definition
web_map_item_data["operationalLayers"][0] = layer

## update the WebMap
item_properties = {"text":web_map_item_data}
web_map_item.update(item_properties=item_properties)

All the best, Glen Final Draft Mapping

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.