English
Continue in the app
Be The First To Get Support Updates
Want to know about the latest technical content and software updates?

How To: Auto-populate attribute fields in a hosted feature layer using Python

Summary

When adding new features to data layers, attributes can be added and edited too. When working with a geodatabase, attribute rules can be used to automatically populate attributes, restrict invalid edits during edit operations, and perform quality assurance checks on existing features. Feature layers can also be published with attribute rules as registered data to your Enterprise environment. By doing this, the attribute rules are applied when edits are made to the referenced layer.

Note:
Attribute rules are not yet supported in ArcGIS Online. When collecting data to a layer hosted on ArcGIS Online, it may be desirable to automatically populate an attribute field. There is the option to assign default values to an attribute field, use the feature templates that enable assigning different default values for each new feature type, or leverage Smart Form capabilities in ArcGIS Field Maps. However, these options do not allow for dynamic calculation. For instance, it may be necessary to calculate the values in an attribute field using the values entered in another field. Some apps such as ArcGIS Survey123 allow using calculations to do this during the data collection. This functionality is also on the roadmap for ArcGIS Field Maps and will be added in the future releases of the app. See the Esri Community blog: Support for Attribute rules in ArcGIS Online 

This can be also done post data collection by editing data in the attribute table. You can use Field Calculator in ArcGIS Online or Desktop to calculate values for any of the attribute fields.

Procedure

Use ArcGIS API for Python to update the attributes for the features in a hosted feature layer. The sample script below calculates the values in a field named "TargetField" for a subset of features (with dates after Jan 15, 2022 in the "DateField" field) by concatenating existing values in two other fields (i.e. "Field1" and "Field2").  If the "TargetField" has existing values, they are overwritten by the new values after updating features with this script. 

#log in to Portal/ArcGIS Online
from arcgis.gis import GIS
gis = GIS('https://arcgis.com/', "Username", "Password")

#find the hosted feature layer
hfl = gis.content.get("ItemID")
hflayer =hfl.layers[0]

#query the features that need to be updated
fset = hflayer.query(where = 'DateField > DATE \'2022-01-15\'')

# updating selected features' attribute
for f in (fset.features):
  f.attributes['TargetField'] = str(f.attributes['Field1']) + "_" + str(f.attributes['Field2'])
  hflayer.edit_features(updates = fset.features)

print ('Attributes were successfully updated!')

This process can be automated by running the script using a third-party app like Windows Task Scheduler or use ArcGIS Notebooks Tasks: 
ArcGIS Online: Schedule a notebook task

Related Information

Last Published: 9/15/2022

Article ID: 000027245

Software: ArcGIS API for Python 2.0.1, 2.0, 1.9.1, 1.9, 1.8.5, 1.8.4, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6 ArcGIS Online Current ArcGIS Pro 3.0.1, 3.0, 2.9.4, 2.9.3, 2.9.2, 2.9.1, 2.9, 2.8.7, 2.8.6, 2.8.5, 2.8.4, 2.8.3, 2.8.2, 2.8.1, 2.8