HOW TO

Download feature service items from ArcGIS Online using ArcGIS API for Python

Last Published: October 17, 2022

Summary

When working with feature services in ArcGIS Online, it is useful to create a copy of all feature services in ArcGIS Online as backup or if there are storage space limitations. Downloading each feature service individually can be time consuming, especially in large volumes. The following Python script allows users to download a copy of all feature service items. The items can be safely deleted once the process is complete.

Procedure

The following script performs a search of the user's feature service items, and exports the items as a single file in the desired format. Once the file is created in ArcGIS Online, it can be downloaded to the file path specified in the download section of the script.

Note:
The item.export() function is available only to users with an organizational subscription. This function is invokable only by the service item owner or an administrator.
  1. In Python, import the necessary libraries.
import arcgis
from arcgis.gis import GIS
  1. Define the login credentials.
gis = GIS(None,'username', 'password', verify_cert=False)
  1. Define the download format and the specified output file path. 
def downloadUserItems(owner, downloadFormat):
    try:
        # Search items by username
        items = gis.content.search('owner:{0}'.format(owner))
        print(items)
        # Loop through each item and if equal to Feature service then download it
        for item in items:
            if item.type == 'Feature Service':
                result = item.export('sample {}'.format(item.type), downloadFormat)
                result.download(r'file path of where to store the download')
                # Delete the item after it downloads to save space (OPTIONAL)
                result.delete()
    except Exception as e:
        print(e)
  1. Download the file generated.
downloadUserItems('username', downloadFormat='Shapefile')

The following is a sample full script:

import arcgis
from arcgis.gis import GIS

gis = GIS(None,'username', 'password', verify_cert=False)

# Download all data from a user
def downloadUserItems(owner, downloadFormat):
    try:
        # Search items by username
        items = gis.content.search('owner:{0}'.format(owner))
        print(items)
        # Loop through each item and if equal to Feature service then download it
        for item in items:
            if item.type == 'Feature Service':
                result = item.export('sample {}'.format(item.title), downloadFormat)
                result.download(r'file path of where to store the download')
                # Delete the item after it downloads to save on space
                result.delete()
    except Exception as e:
        print(e)


# Function takes in two parameters. Username and the type of download format
downloadUserItems('username', downloadFormat='Shapefile')

The downloadUserItems function receives two parameters:

  • owner[string] - for the username
  • downloadFormat[string] - the format for the download output file
Note:
There are multiple file formats for the output and these include, but are not limited to - Shapefile (SHP), CSV, KML, File Geodatabase, Feature Collection. Refer to API Reference for the ArcGIS API for Python, and scroll down to the export method.

Article ID: 000018909

Software:
  • ArcGIS Online
  • ArcGIS API for Python
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options