Setting field visibility not persisting?

Discussion created by rdunfey on Feb 18, 2012
Latest reply on May 24, 2013 by tsmcneel
I've written a python script that I want to run before I publish a mxd as a map service, i want to make sure all Object ID fields are hidden, and all geometry fields are visible.

I've added some messages and the script runs fine, no problem, but it doesn't alter the visibility of the fields for the layers in my map document?

I've attached my code if anyone has time to take a look.

Any ideas much appreciated.

Kind Regards,


import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    # Create a describe object
    desc = arcpy.Describe(lyr)

    # If a feature layer, continue
    if desc.dataType == "FeatureLayer":

        # Create a fieldinfo object
        fieldInfo = desc.fieldInfo
        index = 0
        fldlistgeom = arcpy.ListFields(lyr,"","Geometry")
        fldlistoid = arcpy.ListFields(lyr,"","OID")

        arcpy.AddMessage("Now processing layer: " + desc.Name)

        # Use the count property to iterate through all the fields    
        while index < fieldInfo.count:
            # Print fieldinfo properties to the screen
            if fldlistgeom[0].name == fieldInfo.getFieldName(index):
                arcpy.AddMessage("Setting " + fldlistgeom[0].name + " as visible...")
                fieldInfo.setVisible(index, "VISIBLE")
                arcpy.AddMessage("Geometry field Visible.")
            if fldlistoid[0].name == fieldInfo.getFieldName(index):
                arcpy.AddMessage("Setting " + fldlistoid[0].name + " as hidden...")
                fieldInfo.setVisible(index, "HIDDEN")
                arcpy.AddMessage("Object ID field hidden.")
            index += 1