Solved! Go to Solution.
import os, arcpy #here's an example of how to run through all the layers in an mxd def SetDefQueries(mxdobject, defquery): fixed_layers = [] for layer in arcpy.mapping.ListLayers(mxdobject): #this should weed out the group layers and rasters if layer.supports("DEFINTIONQUERY"): fixed_layers.append(layer.name) layer.definitionQuery = defquery #have the function return a list summary of the layers that were changed (for fun) return fixed_layers #create and print query query = '"VALIDATIONSTATE" = 3' #may have to fix the syntax of this statement depending on the field type arcpy.AddMessage(query) #to go through all of the folder, use os.walk(), you can find documentation on the python website for path, dirs, files in os.walk(top/folder/path): for f in files: if f.endswith(".mxd"): #this should get you all of the mxds in all of the folders. mxd_path = path + "\\" + f arcpy.AddMessage(mxd_path) #now you can make arcpy.mapping.MapDocument() objects mxd = arcpy.mapping.MapDocumentmxd_path) #use the SetDefQuery function on the mxd changed_layers = SetDefQuery(mxd, query) #print a summary of what happened arcpy.AddMessage("In {}:".format(mxd_path)) lyrs_summary = "\n ".join(changed_layers) arcpy.AddMessage(lyrs_summary) #save the mxd at the end (won't work if you have it open) mxd.save()
import os, arcpy #here's an example of how to run through all the layers in an mxd def SetDefQueries(mxdobject, defquery): fixed_layers = [] for layer in arcpy.mapping.ListLayers(mxdobject): #this should weed out the group layers and rasters if layer.supports("DEFINTIONQUERY"): fixed_layers.append(layer.name) layer.definitionQuery = defquery #have the function return a list summary of the layers that were changed (for fun) return fixed_layers #create and print query query = '"VALIDATIONSTATE" = 3' #may have to fix the syntax of this statement depending on the field type arcpy.AddMessage(query) #to go through all of the folder, use os.walk(), you can find documentation on the python website for path, dirs, files in os.walk(top/folder/path): for f in files: if f.endswith(".mxd"): #this should get you all of the mxds in all of the folders. mxd_path = path + "\\" + f arcpy.AddMessage(mxd_path) #now you can make arcpy.mapping.MapDocument() objects mxd = arcpy.mapping.MapDocumentmxd_path) #use the SetDefQuery function on the mxd changed_layers = SetDefQuery(mxd, query) #print a summary of what happened arcpy.AddMessage("In {}:".format(mxd_path)) lyrs_summary = "\n ".join(changed_layers) arcpy.AddMessage(lyrs_summary) #save the mxd at the end (won't work if you have it open) mxd.save()
#use the SetDefQuery function on the mxd if layer.definitionQuery == True: #if a definitionQuery Exists... addquery = layer.definitionQuery + "AND" + query + "=" + "\"" + value + "\"" #Add new to old else: changed_layers = SetDefQueries(mxd, query)
"NameError: name 'layer' is not defined"Is this right? Even though this should have been defined with 'defquery' earlier?
#just copy and paste this into the corresponding place in the SetDefQuery function if layer.supports("DEFINITIONQUERY"): fixed_layers.append(layer.name) #get existing definition query exdefquery = layer.definitionQuery #if the existing query is empty, set the normal defquery if exdefquery == "": layer.definitionQuery = defquery #otherwise, make a combination defquery else: layer.definitionQuery = "{0} AND {1}".format(exdefquery, defquery)