I have an ArcGIS toolbox script that applies a user-defined definition query to multiple layers within the current MXD.

The only user variable currently required is the definition query itself, which I have listed as an "SQL expression". The script works fine, but it would be nice if the SQL validation box on the toolbox GUI was populated with the details of the first layer in the MXD, allowing the user to construct and validate a definition query against the first layer.

How can I achieve this using a hard-coded variable pointing to the first layer in the TOC?

It's fairly straightforward in a one-to-one scenario, by asking the user to input the feature layer as a parameter and then making the SQL expression parameter "Obtained from", but I can't figure out how to do it by defining it myself.


I think this is what you're looking for:

# stores current map doc in a variable
mxd = arcpy.mapping.MapDocument("CURRENT")

# accesses the first layer in mxd
lyr = arcpy.mapping.ListLayers(mxd)[0]
| improve this answer | |
  • Thanks. I already had the code to access the first layer. It was making another parameter dependent upon it that I was struggling with. I have a solution though which I will post below. – Rich Mar 7 '19 at 9:58

I eventually figured this out with some help from an ESRI trainer.

The key was to feed the hardcoded parameter in using tool validation at the initialisation stage and list it as a parameter of type "derrived" (which hid it on the GUI) and then make the user input parameter "obtained from" it.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.