Rearrange field order using python

1040
3
04-11-2018 06:24 AM
JordanMiller4
Occasional Contributor III

I've searched the forums but I can't find any python examples to change field order for layers in the ArcGIS Desktop 10.6 Table of Contents. Is there another way to automate this process?

Tags (3)
0 Kudos
3 Replies
JoeBorgione
MVP Esteemed Contributor

You'll probably need to take a look here to begin with: MoveLayer—Help | ArcGIS Desktop 

While your at it, get familiar with: MapDocument—Help | ArcGIS Desktop & ListLayers—Help | ArcGIS Desktop 

That should just about do it....
0 Kudos
JordanMiller4
Occasional Contributor III

Hi Joe,

I looked at the links and I not quite sure if those links are able to rearrange fields in the layer properties. I am importing several FC layers into a fresh map document and I was hoping to change the attribute field order in which they will appear. I know we can change them manually but I was hoping there would be a way in python to rearrange the field order and save the map document, similar to importing FC layers and saving. I'll provide the code and image below.

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Users\jnmiller\Desktop\Rebuild Map py Scripts\Untitled.mxd")

# Input Variables
#
path = r"C:\Users\jnmiller\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\SDE.sde" # path source w/layers

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

# Add layer to TOC
#
fullPath = os.path.join(path, "ProposedMainlineProjects\ProposedMainlineProjects")
addLayer = arcpy.mapping.Layer(fullPath)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

fullPath = os.path.join(path, "P_PipeSystem\P_Meters")
addLayer = arcpy.mapping.Layer(fullPath)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

fullPath = os.path.join(path, "P_PipeSystem\P_MeterSetting")
addLayer = arcpy.mapping.Layer(fullPath)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

fullPath = os.path.join(path, "P_PipeSystem\P_Regulator")
addLayer = arcpy.mapping.Layer(fullPath)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")‍‍‍‍‍‍‍‍‍‍‍

# Set Visable = False
#
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name
    lyr.visible = False
    
mxd.save()
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JoeBorgione
MVP Esteemed Contributor

I'm sorry, I guess I got my wires crossed when I read "...order for layers in the ArcGIS Desktop 10.6 Table of Contents".  Now I understand what you really would like to do.  I just googled 'arcpy reorder fields'.  Give that a shot: there are several returns.

That should just about do it....
0 Kudos