Definition Query inside a layout's web map

490
1
02-06-2023 08:36 PM
MichaelBell
New Contributor III

I'm trying to create a web tool which will access a layout. The layout contains a web map which already displays some layers, and I'd like to set the definition query on them to only show certain features.

However, for some reason this isn't working. I've managed it in other scenarios, such as loading up a web map with a blank mapframe and adding the layers to that, but I'd like it to use a web map this time in order to make editing it easier.

In any case, for some reason it seems to ignore the definition query and return the same number of features, as if the definition query isn't active (I have checked it, it is active, the layer supports definition queries and this is the only definition query on it). Code snippet below:

 

 

 

lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "WEBMAP_MAP_FRAME")[0]
mfm = mf.map
myLayer = mfm.listLayers("UseThisLayer")[0]
myLayer.definitionQuery = "ThisId = 'abc'"
with arcpy.da.SearchCursor(myLayer, "Shape@") as cursor:
    for row in cursor:
        if row[0]:
            arcpy.AddMessage(row[0])

 

 

 

As I say, I can put anything into the definition query and it still returns the same amount of values, as if it's not there. And I can interrogate the layer's definition query, see that it is active, supported and the only one there.

I do have another way round this (which would be defining the layers outside of the layout, then adding them to the layout's mapframe) but as I say, I'd really like not to have to do this if I can avoid it.

Edit - should point out that this is using ArcGIS Pro, but the layout (and web map and layers) are on my Portal. Eventually this will become a web tool.

Another edit - using the below:

arcpy.AddMessage(myLayer.listDefinitionQueries())

Gives us:

[{'name': 'Query 1', 'sql': "ThisId = 'abc'", 'isActive': True}]
0 Kudos
1 Reply
Kepa
by Esri Contributor
Esri Contributor

Hello @MichaelBell,

I think the problem is that you are executing the tool inside the same project environment. Seems kind of limitation when the project is already opened. I can reproduce your error that way. When executing it from another project it correctly filters/queries (v2.9.X and above) without any issues.

Probably you'll have to set absolute paths in the script and then execute it from another ArcGIS PRO project/instance.

Hope that helps!

Regards,

0 Kudos