Editing layer field properties

6682
12
12-30-2015 08:58 AM
DamianoMorosi1
New Contributor II

Hi all,

I would modify the properties of all layers in an mxd setting, for instance, all the fields as non editable.

The following script seems to work:

import arcpy   

mxd = arcpy.mapping.MapDocument('path-to-mxd')

df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

layers = arcpy.mapping.ListLayers(mxd,"*",df)

for lyr in layers:

    print "Working on layer: " + lyr.name

    fields = arcpy.ListFields(lyr.dataSource)

    for f in fields:

        f.editable = False

mxd.save()

It runs without errors, prints all the layer names but when I open the mxd nothing has changed, the fields are still editable. By the way the mxd IS saved, I just see a new timestamp in the "last modified" property of the file.

Am I missing something?

Thank you,

Damiano

0 Kudos
12 Replies
jameshickey
Occasional Contributor

Have you come up with a solution to this?

I too have fields that I would like to change the editable value for. In my case I need to make a read only field into an editable field.

The change can be made in the field properties of the layer without changing the actual feature class. I know this because I add the feature class to a clean map and the fields are set to NOT read only.

Is this possible to iterate over the entire map and change all fields to "editable = true" with python? 

0 Kudos
DanPatterson_Retired
MVP Emeritus

examine the arcpy field section ... it is editable

editable (Read and Write) ... The editable state: True if the field is editable.  ...Boolean

0 Kudos
jameshickey
Occasional Contributor

Hello and thanks for your reply.

I saw that and have been working with it for a couple of hours now.

Still cant seem to access the layers in the map specifically. 

import arcpy
#Set env workspace
mxd = arcpy.mapping.MapDocument(r"C:\Users\billid\Desktop\WaterUtilityNetwork\WaterSystem.mxd")

#Get layers
for lyr in arcpy.mapping.ListLayers(mxd):
 #Get fields
 fields = arcpy.ListFields(lyr)
 for field in fields:
    print(field)

I just get this error.

Traceback (most recent call last):
File "D:\PythonWorkingFolder\GIS_Python\ListLayers.py", line 8, in <module>
fields = arcpy.ListFields(lyr)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\__init__.py", line 1138, in ListFields
return gp.listFields(dataset, wild_card, field_type)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\geoprocessing\_base.py", line 346, in listFields
self._gp.ListFields(*gp_fixargs(args, True)))
IOError: "Water System" does not exist  

ListFields is supposed to take a string. ListLayers returns <class 'arcpy._mapping.Layer'> not a string

Note that "Water System" is a layer group, but even when I un-group all the layers it just tells me the first layer doesn't exist. I'm guessing it is because it's not a string.

I don't want to work on the data source as it is already set "editable  = true" the map layer properties have it set to false and I would like to change the map and then I can overwrite the .lyr file. Yes I can do it manually but I have many maps and need to check all the fields.

Thanks again for your feed back.   

EDIT: I suppose if i could turn the layer names into text I could make a list out of them....

0 Kudos