[GIS] Programmatically Populate Value List in ArcGIS Tool Interface

arcpyparameterspython-script-toolvalue-list

I'm just trying to set up the parameters in a tool I've built in ArcGIS. What I want to do is read in a feature class or table and get the values in a specific field called "SITE". The field contains a bunch of site names. What I want to do is read in the values of that field and populate them into the 'Value List' tool parameter property

I've been looking at the tool properties and I though I can use the 'Obtained From' property, but it looks like I can only obtain field names, but not that values in the field.

Is there anyway else to do this?

Is there anyways to read in the values from the SITE field and populate it to a value list programmatically?

Best Answer

You're not really supposed to do this, but you could open a cursor in your validator's updateParameters method.

if self.params[0].value and arcpy.Exists(self.params[0].value):
    value_set = set(row.getValue('COL_NAME')
                    for row in arcpy.SearchCursor(str(self.params[0].value))))
    self.params[1].filter.list = sorted(value_set)

This will take some feature layer/table from the first parameter and populate the second parameter's dropdown with a list of values in its COL_NAME field.

Note: this will greatly slow down your validator and is highly discouraged.

Related Question