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

640
2
Jump to solution
01-29-2013 04:57 AM
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)
Reply
0 Kudos
1 Solution

Accepted Solutions
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

Reply
0 Kudos
2 Replies
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

Reply
0 Kudos
New Contributor III
Thats it! Thanks
Reply
0 Kudos