Change display field expression using ArcPy

343
5
Jump to solution
02-18-2021 03:55 AM
CarstenAndersson
New Contributor III

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
New Contributor III

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

0 Kudos
5 Replies
DanPatterson
MVP Notable 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
New Contributor III

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 Notable 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.

View solution in original post

CarstenAndersson
New Contributor III

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

0 Kudos