How do I use Python to test whether a FeatureClass has GlobalIDs?

1716
5
10-06-2011 06:11 AM
GerryReidel
New Contributor II
I have some large SDE databases that we want to start replicating. The feature classes need  Global IDs to do this. Some already have them. I want to write a script that will go through every feature class and add a Global IDs, but if a FeatureClass already has one, the script ends. I know I can test a Table for hasGlobalID, but it doesn't work on a FeatureClass. How can I write an IF statement that will test whether a FeatureClass has a GlobalID? Does anyone have a better suggestion?
0 Kudos
5 Replies
RobertHu
New Contributor II
ListFields function of ArcPy should return you all fields from a feature class.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/ListFields/000v0000001p000000/
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how to do this:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\VECTOR.sde"

for fc in arcpy.ListFeatureClasses("*"):
    for field in arcpy.ListFields(fc, "", "GlobalID"):
        if field.name == "GlobalID":
            print fc + " contains GlobalIDs"

for fd in arcpy.ListDatasets("*"):
    for fc in arcpy.ListFeatureClasses("*", "", fd):
        for field in arcpy.ListFields(fc, "", "GlobalID"):
            if field.name == "GlobalID":
                print fc + " contains GlobalIDs"


The above code will search all stand-alone feature classes, and feature classes with feature datasets to see if the GlobalID field exists.  You will just need to update 'env.workspace' to your SDE connection file.
0 Kudos
forestknutsen1
MVP Regular Contributor

I am wanting to do this as well. Your posted solution does not feel very robust to me because it is based only on the name of the field. I was able to add a GlobalID text field to a fgdb feature class. This would be a false positive in your code. I looked over the field properties for a better solution, but I don't see one. It would be nice to have a function that returns a boolean--something like: hasGlobalID(table/feature class) = True/False.

We want to add global ids to all ersi objects in our database but some have existing global ids.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

There is just a function, or really a property:  GDB Table properties—ArcPy Functions | ArcGIS Desktop 

Properties

PropertyExplanationData Type
globalIDFieldName
(Read Only)

The name of the GlobalID field.

String
hasGlobalID
(Read Only)

Indicates whether the table has a GlobalID field.

Boolean
forestknutsen1
MVP Regular Contributor

Thanks Joshua!

0 Kudos