I'm encountering a strange roadblock and was looking to see if anyone else has experienced this.
I am trying to write a python script that will turn some of my fields to "read only" within my ArcPro project. My current script that that I have written will turn all fields to "read only" for the sake of testing. I have added a message to double check the properties of the field to confirm that it has been set to "read only." However in my current project, my fields still show as unchecked for the "read only" property.
Has anyone else been able to configure a script that would allow fields to retain this setting in their project? I was thinking maybe it had something to do with saving my project? Below is a copy of my script. Any help would be greatly appreciated!
import arcpy # Set variables project = arcpy.mp.ArcGISProject('CURRENT') map = project.listMaps('Map') layer = arcpy.GetParameterAsText(0) desc = arcpy.Describe(layer) for field in desc.fields: field.editable = False arcpy.AddMessage(field.editable)
Updating a field property only updates the field object, no changes are made to the actual field in the table or feature class.
You would have more luck using
where you can alter field properties as well as field names
You can use the cim object to change the field visibility and editable properties
CIMLayerAction Class—ArcGIS Pro
listOfEditableFields = ['field1', 'field2']
listOfVisibleFields = ['field1', 'field2', 'field3']
arprxFile = r"<path to aprx>"
aprx = arcpy.mp.ArcGISProject(arprxFile)
maps = aprx.listMaps()
for map in maps:
for lyr in map.listLayers():
desc = arcpy.Describe(lyr)
l_cim = lyr.getDefinition('V2')
if desc.dataElement.dataType == "FeatureClass":
featureTable = l_cim.featureTable
for fieldDesc in featureTable.fieldDescriptions:
fieldDesc.visible = False
fieldDesc.readOnly = False
for visibleFieldName in listOfVisibleFields:
if fieldDesc.fieldName == visibleFieldName:
fieldDesc.visible = True
for editableFieldName in listOfEditableFields:
if fieldDesc.fieldName == editableFieldName:
fieldDesc.readOnly = True
if __name__ == '__main__':