As part of the validation for parameters of a Python toolbox, I need to check if the text in parameter #1 is exists in a layer in a MXD file selected as parameter #2. Currently, I use this code in updateMessages
:
mxd = arcpy.mapping.MapDocument(parameters[2].valueAsText)
layer = arcpy.mapping.ListLayers(mxd, "LayerName")[0]
condition = u"Field=" + parameters[1].text
with arcpy.da.SearchCursor(layer.dataSource, ["Field"], condition) as cursor:
#Do some stuff...
The problem is that this takes very long time since the rather large MXD file has to be opened. It is not acceptable to have a two minute waiting time with ArcMap freezing every time the parameter validation is performed.
So my question is this: Is there any way to find the source of the layer, and search for a value in it, without having to open the MXD?
ListLayers
that takes most of the time. I managed to reduce the time by reducing the number of layers (in my case, by merging rasters).