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.

Discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience with using the ArcGIS API for Python to perform a wide range of content management tasks with ease, such as creating Folders and Groups and managing content within them.

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)
				
			

Automate ArcGIS Online WebMap 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 WebMap 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 WebMaps and the ArcGIS API for Python.

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