Create a CSV Content Item Directly in ArcGIS Online using the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating components of ArcGIS Online such as content items. In this post we will use the ArcGIS API for Python to create a CSV content item directly in ArcGIS Online without having to created the CSV file on disk first and uploading.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will 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 in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

arcgis modules and standard Python csv

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. 

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import the standard Python csv module
import csv

				
			

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. This is handled in the code snippet above.

				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Create the CSV Headings

We create the desired headings for our CSV. I am going to use the CSV to store the name, type and id of each content item in my ArcGIS Online. 

				
					## Create the headings. I want to store information about items so I can
## use the CSV in automated workflows.
headings = ["NAME", "TYPE", "ITEM_ID"]
				
			

Define the CSV filepath

When using the csv module we need to define where the CSV file will be saved. We use the tmp folder in ArcGIS Online, a behind the scenes folder. Yow will see where this filepath came from later on in the workflow. For now, run with it.

				
					## we need a filepath where we will save the csv
## we use the AGOL tmp folder, you will see where this comes from later on.
csv_filepath = "/tmp/agol_items_lookup.csv"
				
			

Create the CSV and add rows

We write are column headers and then use the ContentManager search() method to iterate through the ArcGIS Online content items and add information for each as a row in the CSV.

				
					## Create the CSV with headings and populate with rows that contain
## each item name, type, and id
with open(csv_filepath, mode="w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(headings)
    for item in agol.content.search("", max_items=100):
        write.writerow([item.title, item.type, item.id])
				
			

Define CSV item properties

Define the CSV item properties that are displayed on the content item homepage.

				
					## define the item properties
csv_properties = {
    "title" : "AGOL_Item_Lookup",
    "tags" : ["csv", "items", "lookup"],
    "snippet" : "A catalogue of ArcGIS Online items", # summary
    "description" : "A catalogue of ArcGIS Online items for aiding with automated workflows with the ArcGIS API for Python",
    "type" : "CSV"    
}
				
			

Add CSV as a content item

We call upon the ContentManager add() method to add our CSV as a content item in ArcGIS Online. We supply the csv_properties defined above and the path to our CSV.

				
					## use the ContentManager add() method to add as a Content Item
csv_agol = agol.content.add(item_properties=csv_properties, data=csv_filepath)
				
			

Check to see that the item has an id inicating a successful creation of the content item.

				
					## show the Itemd ID to see that it is now a commited Content Item
csv_agol.id
				
			

We can also check our content to see the item has been created.

Where did we get the tmp filepath from?

How did we know to use the tmp folder? You can get this location from the item object get_data() method for a CSV content item.

				
					## here's where we get the path to the CSV file in AGOL
csv_filepath = csv_agol.get_data()
print(csv_filepath)
				
			

Print the contents of the CSV to screen

Lets print the contents of our newly created CSV item to screen.

				
					## use that path to access the CSV file and print the contents
with open(csv_filepath, mode="r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
				
			

See the next blog post to update the CSV directly in ArcGIS Online.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will 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 in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

All the code in one place

You can find the entire code workflow below.

				
					from arcgis.gis import GIS
import csv

agol = GIS("home")

## Create the headings. I want to store information about items so I can
## use the CSV in automated workflows.
headings = ["NAME", "TYPE", "ITEM_ID"]

## we need a filepath where we will save the csv
## we use the AGOL tmp folder, you will see where this comes from later on.
csv_filepath = "/tmp/agol_items_lookup.csv"

## Create the CSV with headings and populate with rows that contain
## each item name, type, and id
with open(csv_filepath, mode="w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(headings)
    for item in agol.content.search("", max_items=100):
        writer.writerow([item.title, item.type, item.id])
        
## define the item properties
csv_properties = {
    "title" : "AGOL_Item_Lookup",
    "tags" : ["csv", "items", "lookup"],
    "snippet" : "A catalogue of ArcGIS Online items", # summary
    "description" : "A catalogue of ArcGIS Online items for aiding with automated workflows with the ArcGIS API for Python",
    "type" : "CSV"    
}

## use the ContentManager add() method to add as a Content Item
csv_agol = agol.content.add(item_properties=csv_properties, data=csv_filepath)

## show the Itemd ID to see that it is now a commited Content Item
print(csv_agol.id)

## here's where we get the path to the CSV file in AGOL
csv_filepath = csv_agol.get_data()
print(csv_filepath)

## use that path to access the CSV file and print the contents
with open(csv_filepath, mode="r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
				
			

1 thought on “Create a CSV Content Item Directly in ArcGIS Online using the ArcGIS API for Python”

  1. Pingback: Read and Write to a CSV Content Item in ArcGIS Online using the ArcGIS API for Python – FDM Geospatial Academy

Leave a Comment

Your email address will not be published. Required fields are marked *