Using arcpy to List Domains Assigned to Featureclass Fields

I was making an edit (adding leading “0”s) to a coded-value domain in an SDE database and realized that my edits were changing the order of the rows of my domain.  Rows were moved to the bottom of the list when they were edited.

So I went through the process of converting my domain back to a table, made my edits in Access and exported the rows to a .dbf in the order I wanted them.

I removed the domain from the field I knew it was assigned to but when I tried to delete the domain, I received an error (The domain is used as a default domain).

The Google Machine led me to an ArcForums post by  with some python code for listing all the fields with a domain.

I tweaked the original a bit, first because it was only examining feature classes in a feature dataset, not stand-alone feature datasets.  And secondly, I updated it to use arcpy.  I posted both the 9.3 version and the 10.0 version, but here is a quick look.  The key addition is the ‘listfc(“”)’ line that is the first line of the def listds() module.

import arcpy

min_workspace = r"C:\Users\mranter\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\min.minstaff.sde"

infgdb=(min_workspace)
arcpy.env.workspace = infgdb

def listfc(inDataset):
   featureclasses = arcpy.ListFeatureClasses("","",inDataset)
   for f in featureclasses:
      print "feature class: ",f

lfields=arcpy.ListFields(f)

for lf in lfields:
   if lf.domain < "":
      print "      domain",f, lf.name, lf.domain

def listds():
   listfc("")

   datasets=arcpy.ListDatasets ("","")
   for d in datasets:
      print "  dataset: ",d

listfc(d)
listds()

One thought on “Using arcpy to List Domains Assigned to Featureclass Fields

  1. I made a nice script that will search every field of every table and feature class to see if the given domain is applied. I tested this on ArcGIS 10.2.1. We were able to find we have a DOUBLE type domain applied to a FLOAT type field. We also see that there’s no way to apply or remove domains on a FLOAT type field! So now that this script shows we have the domain applied to the field, there’s no way in the ArcCatalog UI to remove or change the domain that’s applied!

    import arcpy

    #path to your .sde file with user/password SAVED
    gis_workspace = r”C:\temp\geod as DIST.sde”

    #name of domain you’re looking for
    domainName = “Transformer Unit KVA”;

    infgdb=(gis_workspace)
    arcpy.env.workspace = infgdb

    def listfc(inDataset):
    print “checking all feature classes…”
    featureclasses = arcpy.ListFeatureClasses(“”,””,inDataset)
    for f in featureclasses:
    #print “checking feature class: “,f
    lfields=arcpy.ListFields(f)
    for lf in lfields:
    #if lf.domain > “”:
    #print “Field”,lf.name,” domain: “,lf.domain
    if lf.domain == domainName:
    print “!!!found domain applied to: “,f,”–>”, lf.name

    def listtables():
    print “checking all tables…”
    tables = arcpy.ListTables()
    for t in tables:
    #print “checking table: “,t
    if t != “DIST.PLAN_TABLE”: #Santee Cooper has a bad table to skip
    lfields=arcpy.ListFields(t)
    for lf in lfields:
    #if lf.domain > “”:
    #print “Field”,lf.name,” domain: “,lf.domain
    if lf.domain == domainName:
    print “!!!found domain applied to: “,t,”–>”, lf.name

    def listds():
    listfc(“”)
    listtables()

    #main function
    print “checking for domain: “, domainName
    listds()

Comments are closed.