I’m trying to create a simple search tool so that my colleagues can easily query a database. My first version is like this.
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
SearchTerm = arcpy.GetParameterAsText(0)
Fuzzy = arcpy.GetParameterAsText(1)
if Fuzzy == False:
NewLayer = arcpy.MakeFeatureLayer_management(r"W:Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME LIKE '%" + SearchTerm + "%'")
else:
NewLayer = arcpy.MakeFeatureLayer_management(r"W:Source.lyr","Search Results (" + SearchTerm + ")","PROC_NAME = '" + SearchTerm + "'")
This works as expected if I just run the code through the python window (with the parameters hardcoded) but if I run it as a script in toolbox nothing happens. I suppose ArcMap doesnt automatically add the layer you create in a script, so I’ve added:
arcpy.mapping.AddLayer(df,NewLayer,"TOP")
This doesn’t give add a layer to my mxd and gives me the error message: Runtime error <type 'exceptions.AssertionError'>:
Surely this should be pretty simple. Is there a better way of doing it?