Select to view content in your preferred language

Setting field visibility not persisting?

3176
3
02-18-2012 10:58 AM
RobDunfey
Regular Contributor
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,

Rob


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



arcpy.AddMessage("OK")


mxd.save()
Tags (2)
0 Kudos
3 Replies
AlessandroCinnirella
Regular Contributor
try putting arcpy.RefreshActiveView() at the end of the script

ciao,
AC
0 Kudos
RobDunfey
Regular Contributor
Hi Alessandro,

Thanks for the suggestion.  I tried this but unfortunately no success.  I think this may be a bug.

Thanks,

Rob
0 Kudos
TimothyMcNeel
Emerging Contributor
Has anyone found a solution or workaround to this?

Thanks,
Tim
0 Kudos