Get your ArcGIS Online Feature Layer OID Field Name with the ArcGIS API for Python

Table of Contents

The Video

Introduction

The OID field for your feature layer in ArcGIS Online 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 the ArcGIS API for Python we can inspect the JSON for the feature layer and retrieve the objectIdField property value.

Automate ArcGIS Online Feature Service Workflows with the ArcGIS API for Python | A Complete Guide from Beginner to Advanced | Full Course

Free on YouTube! A set of videos detailing ArcGIS Online Feature Services workflows using the ArcGIS API for Python. We will do a deep dive into properties and methods available, along with custom workflows for automating with Feature Services and the ArcGIS API for Python.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. We will also utilise the FeatureLayer class that will enable us to get the OID Field of a Feature Layer in ArcGIS Online.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## the FeatureLayer class
from arcgis.features import FeatureLayer
				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Required inputs

Only one  required parameter, the url to the Feature Layer, although you can access and create the Feature Layer object any way you wish to.

				
					## the URL to the Feature Layer
fl_url = "FEATURE_LAYER_URL"
				
			

Create the FeatureLayer object

We create a FeatureLayer object from the user provided URL. You can create a FeatureLayer object in a number of ways such as using the layers property for a Feature Service Item object or via a FeatureLayerCollection object, The choice is yours.

				
					fl = FeatureLayer(
    url = fl_url,
    gis = agol
)
				
			

Get the objectIdField from the FeatureLayer properties

The FeatureLayer object has a property called properties that holds a host of information and configurations relating to the Feature Layer in ArcGIS Online. We are interested in the objectIdField property that will return the name of the OID field as a string.

				
					oid_field = fl.properties.objectIdField

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.

				
					from arcgis.gis import GIS
from arcgis.features import FeatureLayer

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#featurelayer
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.properties
##
################################################################################


################################################################################
## ACCESS ARCGIS ONLINE ########################################################

agol = GIS("home")

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

## the URL to the Feature Layer
fl_url = "FEATURE_LAYER_URL"

################################################################################
## CREATE A FEATURELAYER OBJECT ################################################

fl = FeatureLayer(
    url = fl_url,
    gis = agol
)

################################################################################
## GET OID FIELD NAME ##########################################################

print(fl.properties.objectIdField)

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

Leave a Comment