Get a Feature Class OID (ObjectID) field Name using ArcPy

Table of Contents

The Video

Introduction

The OID field for your Feature Class in ArcGIS Pro might be called OBJECTID, it could also be OID, FID, or any other name that you have given it. This can make it difficult to create a script to process data that may not have a standard OID field name you expect. With ArcPy we can query the fields for a designated OID field type to access the OID field name.

Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors

Free on YouTube! ArcPy Cursors from the data access module enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro by accessing, inserting, and updating features in your datasets.

User Input

Just the one. The path to the feature class or table.

				
					## the path to the feature class to get the OID field for
fc_path = r"C:\path\to\fc_or_table"
				
			

Get OID Field Name with arcpy.ListFields()

The arcpy.ListFields() function will return all fields as Field objects. We can interrogate the type property to access the OID field.

				
					oid_field = [fld.name for fld in arcpy.ListFields(fc_path) if fld.type=="OID"][0]

print(oid_field)
				
			

Get the OID Field Name with Describe()

We can use the Data Access module (da) and the Describe function to return a dictionary where we can find the OID field name, or we can use the old school arcpy.Describe() function that returns a Describe object.

				
					oid_field = arcpy.da.Describe(fc_path)["OIDFieldName"]
oid_field = arcpy.Describe(fc_path).OIDFieldName

print(oid_field)
				
			

If you’ve found these blogs helpful and would like to support the project, please consider making a donation. All learning material is provided free of charge to help GIS professionals, students, and developers learn ArcPy, the ArcGIS API for Python, ArcGIS Pro, and ArcGIS Online. Donations help cover website hosting, software licensing, domain costs, and the time involved in creating and maintaining tutorials, courses, and learning resources. Your support helps keep this content freely available to everyone and allows new courses and materials to be developed for the GIS community. If the content has helped you solve a problem, learn a new skill, or advance your career, please consider supporting the project with a donation. Every contribution, no matter the size, is greatly appreciated.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					import arcpy

################################################################################
## ArcPy Reference Links:
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfields.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/describe.htm
##
################################################################################

################################################################################
## USER INPUT ##################################################################

## the path to the feature class to get the OID field for
fc_path = r"C:\path\to\fc_or_table"

################################################################################
## GET OID FIELD WITH LISTFILEDS ###############################################

oid_field = [fld.name for fld in arcpy.ListFields(fc_path) if fld.type=="OID"][0]

print(oid_field)

################################################################################
## GET OID FIELD WITH DESCRIBE #################################################

oid_field = arcpy.da.Describe(fc_path)["OIDFieldName"]
oid_field = arcpy.Describe(fc_path).OIDFieldName

print(oid_field)

################################################################################
print("\nSCRIPT COMPLETE")
				
			

Leave a Comment