1

Would someone be able to share the logistics on how to permanently programmatically reorder fields in a feature class with ArcObjects, VB.Net.

I want to be able to do this in the geodatabase not in the IOrderedLayerFields Interface of the Carto assembly.

7

Even with ArcObjects, the only way to permanently reorder fields in a table or feature class is to drop the table completely and re-create it. There is a very popular ArcGIS Idea to get this implemented, so vote for it if you want to see such a feature.

| improve this answer | |
2

This is possible in python using FeatureClasstoFeatureClass with Fieldmappings. You can also rename fields at the same time.

So if you have a Featureclass with FIELD3,FIELD2,FIELD1 and you want the result to be FIELD1,F2,F3 then the following code should accomplish this.

arcpy.env.overwriteOutput = True

input_fpath = "c:\\gis\\Geodatabases\\temp.gdb\\in_fc"

output_dpath = "c:\\gis\\Geodatabases\\temp.gdb"
output_fname = "out_fc"

fms = arcpy.FieldMappings()

fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD1")
fms.addFieldMap(fm)

fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD2")
of = fm.outputField
of.name = "F2"
of.aliasName = "F2"
fm.outputField = of
fms.addFieldMap(fm)

fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD3")
of = fm.outputField
of.name = "F3"
of.aliasName = "F3"
fm.outputField = of
fms.addFieldMap(fm)

arcpy.conversion.FeatureClassToFeatureClass(input_fpath,output_dpath,output_fname,"",fms)
| improve this answer | |
2

I know this is totally out of the scope of the question, but could be useful for those who want to reorder fields inside a table only once by hand. You can do the trick by importing the classes (or the whole geodatabase) into a personal geodatabase. Then open it with Access and reorder the fields (yeah, cool) and export it back to the original format.

Hope it helps

| improve this answer | |
  • I like this out of the box solution. This worked really well for me. – Fezter Oct 27 '15 at 1:26
-1

Reorder Fields

Author: Luke Savage, GISP
Contributor: Brandon Whitney
Created: July 10, 2020
Tested: ArcGIS 10.8

Purpose: To reorder GIS fields that have like fields per feature class and table so those fields are reordered in front of all the rest of the fields. This works on the complete dataset. Make sure fields exist and are aliased properly. Code is Case Sensative

Import Libraries

import arcpy
import arcgis
import pandas
import os
import sys
import traceback
import operator

Declare Workspace

path="D:/temp"
dbname="/gis"
arcpy.env.workspace=path+dbname+'.gdb'
demogis=arcpy.env.workspace
newdb=path+dbname+'_New.gdb'
print(path+dbname+'.gdb')
print(demogis)
print (newdb)

Reorder Fields

Create New File Geodatabase and Feature Datasets

arcpy.management.Delete(path+dbname+'_New'+'.gdb')
arcpy.management.CreateFileGDB(path,dbname + '_New')
try:
    for ds in arcpy.ListDatasets():
        output_dpath = (path+dbname + '_New' + '.gdb')
        arcpy.CreateFeatureDataset_management(output_dpath, ds, ds)
except Exception:
    e = sys.exc_info()[1]
    print(e.args[0], ds)

Reorder Field Mapping in Feature Classes

'pfields' list needs to be updated to fields and alias field names in order. This will set order for the fields within the feature classes. Fields must exist in feature class. Case Sensative
pfields=[('AssetID','Asset Identifier'),('Display','Display Name'),('OpStatus','Operational Status'),('LifeCycleStatus','Lifecycle Status'),('Location','Location Description'),('Address','Address'),('OwnedBy','Owned By'),('MaintBy','Maintained By'),('PrimaryImage','Primary Image'),('CondInspScore','Condition Score'),('CondInspDate','Condition Date'),('WarrantyDate','Warranty Date'),('InstallDate','Install Date'),('InstallCost','Install Cost'),('ExpReplaceDate','Exp Replace Date'),('ExpReplaceCost','Exp Replace Cost'),('Age','Age'),('Criticality','Criticality'),('iPoF','Failure Probability'),('iCoF', 'Failure Consequence'),('iBRE','Business Risk'),('iMSPU','Maint Under Perform'),('iMSPO','Maint Over Perform'),('iMSR','Maint Score'),('iMCFY','Maint Curve Fast Yr'),('iMCSY','Maint Curve Slow Yr'),('iMCAY','Maint Curve Avg Yr'),('iMSName','Maint Strategy Name'),('iCalcDate','OpInsights Calc Date'),('IndFacID','Indoors Facility Identifier'),('IndLevID','Indoors Level Identifier'),('Comments','Comments')]
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        pfield=[item[0] for item in pfields]
        palias=[item[1] for item in pfields]
        desc = arcpy.Describe(fc)
        output_dpath = (path+dbname + '_New' + '.gdb'+'/'+ds)
        input_fpath = demogis+'/'+ds+'/'+fc
        output_fname = fc
        fms = arcpy.FieldMappings()
        fcall = [(f.name,f.aliasName) for f in arcpy.ListFields(demogis+'/'+ds+'/'+fc) if f.editable]
        ffield = [item[0] for item in fcall]
        falias=[item[1] for item in fcall]
        fields2Add = list(set(fcall) - set(pfields))
        try:    
            for pfield,palias in pfields:
                fm = arcpy.FieldMap()
                fm.addInputField(input_fpath,pfield)
                of = fm.outputField
                of.name = pfield
                of.aliasName = palias
                fm.outputField = of
                fms.addFieldMap(fm)
        except Exception:
            e = sys.exc_info()[1]
            print(e.args[0], fc,pfield)
        try:
            for ffield,falias in fields2Add:
                if ffield != "SHAPE" and ffield != 'Shape':
                    fm = arcpy.FieldMap()
                    fm.addInputField(input_fpath,ffield)
                    of = fm.outputField
                    of.name = ffield
                    of.aliasName = falias
                    fm.outputField = of
                    fms.addFieldMap(fm)
        except Exception:
            e = sys.exc_info()[1]
            print(e.args[0], fc,ffield)
        arcpy.conversion.FeatureClassToFeatureClass(input_fpath,output_dpath,output_fname,"",field_mapping=fms)
    

Reorder Field Mapping in Tables

'pfields' list needs to be updated to fields and alias field names in order. This will set order for the fields within the tables. Fields must exist in tables. Case Sensative
pfields=[('AssetID','Asset Identifier'),('Display','Display Name'),('OpStatus','Operational Status'),('LifeCycleStatus','Lifecycle Status'),('Location','Location Description'),('Address','Address'),('OwnedBy','Owned By'),('MaintBy','Maintained By'),('PrimaryImage','Primary Image'),('CondInspScore','Condition Score'),('CondInspDate','Condition Date'),('WarrantyDate','Warranty Date'),('InstallDate','Install Date'),('InstallCost','Install Cost'),('ExpReplaceDate','Exp Replace Date'),('ExpReplaceCost','Exp Replace Cost'),('Age','Age'),('Criticality','Criticality'),('iPoF','Failure Probability'),('iCoF','Failure Consequence'),('iBRE','Business Risk'),('iMSPU','Maint Under Perform'),('iMSPO','Maint Over Perform'),('iMSR','Maint Score'),('iMCFY','Maint Curve Fast Yr'),('iMCSY','Maint Curve Slow Yr'),('iMCAY','Maint Curve Avg Yr'),('iMSName','Maint Strategy Name'),('iCalcDate','OpInsights Calc Date'),('IndFacID','Indoors Facility Identifier'),('IndLevID','Indoors Level Identifier'),('Comments','Comments')]
tables = arcpy.ListTables()
for tb in tables:
    output_dpath = (path+dbname + '_New' + '.gdb')
    input_fpath = demogis+'/'+tb
    pfield=[item[0] for item in pfields]
    palias=[item[1] for item in pfields]
    output_fname = tb
    fms = arcpy.FieldMappings()
    fcall = [(f.name,f.aliasName) for f in arcpy.ListFields(demogis+'/'+tb) if f.editable]
    ffield = [item[0] for item in fcall]
    falias=[item[1] for item in fcall]
    fields2Add = list(set(fcall) - set(pfields))
    try:    
        for pfield,palias in pfields:
            fm = arcpy.FieldMap()
            fm.addInputField(input_fpath,pfield)
            of = fm.outputField
            of.name = pfield
            of.aliasName = palias
            fm.outputField = of
            fms.addFieldMap(fm)
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0], tb,pfield)
    try:
        for ffield,falias in fields2Add:
            if ffield != "SHAPE" and ffield != 'Shape':
                fm = arcpy.FieldMap()
                fm.addInputField(input_fpath,ffield)
                of = fm.outputField
                of.name = ffield
                of.aliasName = falias
                fm.outputField = of
                fms.addFieldMap(fm)
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0], tb,ffield)
    arcpy.conversion.TableToTable(input_fpath,output_dpath,output_fname,"",field_mapping=fms)

Set Feature Class Alias Name To New Geodatabase

arcpy.env.workspace=demogis
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        desc = arcpy.Describe(fc)
        alName=desc.aliasName
        newfc= newdb+'/'+ds+'/'+fc
        arcpy.AlterAliasName(newfc,alName)
    

Set Table Alias Name To New Geodatabase

arcpy.env.workspace=demogis
tables = arcpy.ListTables()
for tb in tables:
    desc = arcpy.Describe(tb)
    alName=desc.aliasName
    newtb= newdb+'/'+tb
    arcpy.AlterAliasName(newtb,alName)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.