Python Script to Set Fields to "Read Only" in ArcGIS Pro

1871
5
07-01-2020 12:48 PM
LauraCarson
New Contributor II

Hi everyone!

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')[0]
layer = arcpy.GetParameterAsText(0)

desc = arcpy.Describe(layer)
for field in desc.fields:
    field.editable = False
    arcpy.AddMessage(field.editable)
5 Replies
DanPatterson
MVP Esteemed Contributor
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

Alter Field—Data Management toolbox | Documentation 

where you can alter field properties as well as field names


... sort of retired...
0 Kudos
LauraCarson
New Contributor II

Thank you, Dan for your response! Unfortunately the Alter Field tool from the Data Management toolbox does not change the read-only property.

0 Kudos
DanPatterson
MVP Esteemed Contributor

My bad... I think critical things probably need to be set up during the creation phase


... sort of retired...
0 Kudos
VinceDoelman
New Contributor

Dear Laura,

I am running into the exact same issue. I was wondering if you've found a solution yet?

Sincerely,

Vince

0 Kudos
stevetaylor_perth
New Contributor II

You can use the cim object to change the field visibility and editable properties
CIMLayerAction Class—ArcGIS Pro

import arcpy

listOfEditableFields = ['field1', 'field2']
listOfVisibleFields = ['field1', 'field2', 'field3']

def main():

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)
print(lyr.name)
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

lyr.setDefinition(l_cim)

del aprx
print("Completed")
if __name__ == '__main__':
main()

0 Kudos