Add Definition Query to all Layers in MXD that reside in SDE only

885
2
Jump to solution
01-29-2013 04:57 AM
jamesborris
New Contributor III
Hi, I am trying to add a definition query to all layers in an MXD that reside in an SDE database and not those that do not. The below code will add the definition query "RID = 111" to all layers in an MXD. How do I manipulate it to only add to those in my SDE database? I tried the wildcard but the names are all different so I was not able to get only the sde layers. Thanks.

import arcpy from arcpy import env env.workspace = "C:/WORKSPACE" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] CRID = 333 defquery = """ "RID" = """ + str(CRID) for lyr in arcpy.mapping.ListLayers(mxd):         lyr.definitionQuery = defquery
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi James,

You can check if the layer resides in an SDE geodatabase by using the Layer's 'dataSource' property.  Ex:

import arcpy from arcpy import env env.workspace = "C:/WORKSPACE" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] CRID = 333 defquery = """ "RID" = """ + str(CRID) for lyr in arcpy.mapping.ListLayers(mxd):         if ".sde" in lyr.dataSource:                lyr.definitionQuery = defquery

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi James,

You can check if the layer resides in an SDE geodatabase by using the Layer's 'dataSource' property.  Ex:

import arcpy from arcpy import env env.workspace = "C:/WORKSPACE" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] CRID = 333 defquery = """ "RID" = """ + str(CRID) for lyr in arcpy.mapping.ListLayers(mxd):         if ".sde" in lyr.dataSource:                lyr.definitionQuery = defquery
0 Kudos
jamesborris
New Contributor III
Thats it! Thanks
0 Kudos