Backup AGO Feature Layers Using Notebook for Organization

224
0
12-08-2022 05:48 AM
Labels (2)
ChristopherMask
New Contributor III

The following inputs will backup all feature layers in the Organization. A zip file is created for each feature layer and saved to the directory /arcgis/home. 

Input 1

from arcgis.gis import GIS
import datetime as dt
gis = GIS('home')

 

Input 2

org_users = gis.users.search(query = '*', max_users = 10000)
print(f'{len(org_users)} users found')
org_users[:]

 

Input 3

org_content = []

for user in org_users:
qe = f"type:Feature Service, owner: {user.username}"
user_content = gis.content.advanced_search(query=qe, max_items=-1)['results']
org_content += user_content

print(f"{len(org_content)} items found in org")
print(org_content)

 

Input 4

folder_path = ('home')
items = org_content
print(items)

 

Input 5

def download_as_fgdb(item_list, backup_location):
for item in item_list:
try:
if 'View Service' in item.typeKeywords:
print(item.title + " is view, not downloading")
else:
print("Downloading " + item.title)
version = dt.datetime.now().strftime("%d_%b_%Y")
result = item.export(item.title + "_" + version, "File Geodatabase")
result.download(backup_location)
result.delete()
print("Successfully downloaded " + item.title)
except:
print("An error occurred downloading " + item.title)
print("The function has completed")

download_as_fgdb(items, folder_path)

Tags (1)
0 Replies