Change display field expression using ArcPy

3002
7
Jump to solution
02-18-2021 03:55 AM
CarstenAndersson
Occasional Contributor

How do I change the display field expression using ArcPy?

CarstenAndersson_0-1613649249261.png

 

0 Kudos
2 Solutions

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

displayField and displayExpressionInfo are mutually exclusive, i.e., use displayField for displaying a field only and use displayExpressionInfo for displaying an expression.  You are passing an expression into displayField, which is why the "$feature" is being prepended.

View solution in original post

CarstenAndersson
Occasional Contributor

Thanks a lot, Joshua! That actual worked. I ended up with the following code:

lyr_def = lyr.getDefinition("V2")
exp_info = arcpy.cim.CIMExpressionInfo()
exp_info.expression = "\"Matr. nr. \"+$feature.MatrikelNr+\" \"+$feature.LandsEjerlav+\" (\"+$feature.LandsEjerlavKode+\")\""
lyr_def.featureTable.displayExpressionInfo = exp_info
lyr.setDefinition(lyr_def)

I really appreciate the help 🙂

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

It isn't supported in arcpy.

There are old threads to this effect

How does ArcMap decide which field is used for the... - GeoNet, The Esri Community

It isn't under arcpy's layer properties or the recent addition of CIM

Python CIM access—ArcGIS Pro | Documentation

I couldn't even find it as an Idea


... sort of retired...
0 Kudos
CarstenAndersson
Occasional Contributor

Hi Dan.

Yeah, I also tried using CIM by setting the displayField property to the required expression. It actually sets it, but it looks strange and doesn't work.

I used this Py code:

lyr_def = lyr.getDefinition("V2")
lyr_def.featureTable.displayField = "\"Matr. nr. \"+$feature.MatrikelNr+\" \"+$feature.LandsEjerlav+\" (\"+$feature.LandsEjerlavKode+\")\""

Which changes it

CarstenAndersson_0-1613655057032.png

 

But for some reason it's adding a $feature. in front of my express. So no luck there. Hopefully this will be added as a possibility in the future.

Thanks for the reply 🙂

0 Kudos
DanPatterson
MVP Esteemed Contributor

$feature .... looks like Arcade to me


... sort of retired...
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

displayField and displayExpressionInfo are mutually exclusive, i.e., use displayField for displaying a field only and use displayExpressionInfo for displaying an expression.  You are passing an expression into displayField, which is why the "$feature" is being prepended.

DAVIDWADSWORTH_LVVWD
New Contributor III

Thanks for contributing to this solution. I had the same question and you have saved me a lot of time.

0 Kudos
CarstenAndersson
Occasional Contributor

Thanks a lot, Joshua! That actual worked. I ended up with the following code:

lyr_def = lyr.getDefinition("V2")
exp_info = arcpy.cim.CIMExpressionInfo()
exp_info.expression = "\"Matr. nr. \"+$feature.MatrikelNr+\" \"+$feature.LandsEjerlav+\" (\"+$feature.LandsEjerlavKode+\")\""
lyr_def.featureTable.displayExpressionInfo = exp_info
lyr.setDefinition(lyr_def)

I really appreciate the help 🙂

DAVIDWADSWORTH_LVVWD
New Contributor III

Thanks for raising this question, and sharing your solution. I was working with this same issue and this has saved a lot of time.

0 Kudos