A script to copy hosted feature layers from one Portal to another Portal on the same network?

1494
2
08-17-2023 04:36 AM
David_Brooks
MVP Regular Contributor

We've tried using AGOL Assistant and ArcGIS Assistant to copy our hosted feature services across from one Portal to another Portal (part of a Portal migration due to a team breaking away from another team). 

ArcGIS Assistant only supports copying referenced items (rest service still pointing back to original portal)

AGOL Assistant copying in "Full" mode is unstable, and either fails for the larger services or doesn't bring the symbology across.

We would like a script that will look at the old feature service, download it as FGDB to a local drive and then publish back up to the new portal, with matching naming, metadata, symbology, and - if possible - the same group sharing (pointing to new identical groups on new portal).

Has anyone done this before?

We would probably then use AGOL Assistant to repoint the layers in the WebMaps to the new URLs, but would be keen to hear other options on this too.


David
..Maps with no limits..
0 Kudos
2 Replies
julian_svcs
Frequent Contributor

You can try use the clone_items to move between environments.

Have a look at this for a step-by-step: https://developers.arcgis.com/python/guide/cloning-content/

 

0 Kudos
VeronicaUrrego
Esri Contributor

Hi, @David_Brooks 

You can try this Python script that involves exporting a hosted feature layer from AGOL to a local File Geodatabase (GDB) and then uploading that GDB back to AGOL. Let's break down each block of the code:

Block 1: Connecting to ArcGIS Online and User Input

  • The outputFolder variable stores the local path where the GDB will be extracted.
  • The username variable captures user input for their ArcGIS Online username.
  • The GIS class is used to create a connection to ArcGIS Online using the provided URL and username.

Block 2: Exporting Hosted Feature Layer from AGOL

  • The item_id variable holds the ID of the hosted feature layer to be downloaded from AGOL.
  • AGOLitem is a reference to the hosted feature layer item on AGOL.
  • The export method is used to export the hosted feature layer to a GDB named Temp_FGB. The export process is waited for (using wait=True), and then there's a delay of 10 seconds before proceeding.

Block 3: Reconnecting to ArcGIS Online or the new portal

This block of code re-establishes a connection to ArcGIS Online with a different URL. 

Block 4: Uploading GDB to ArcGIS Online

  • data_path stores the path to the ZIP file containing the GDB exported in the earlier block.
  • zip_properties define the type of item being added (a File Geodatabase in this case).
  • add_fgdb represents the newly uploaded GDB item on ArcGIS Online

Block 5: Sharing the GDB Item

This block shares the uploaded GDB item with a specific group identified by its ID ("32cc4d232e45464d8c536a076c2f0d70"). Replace this ID with the ID of the group you want to share the information.

 

 

#Block 1: Connecting to ArcGIS Online and User Input
outputFolder=r"C\Test_fGDB" #replace this with the path where the GDB will be extracted to
username = input("Input your username: ")
gis = GIS("https://arcgis.com", username) #replace these with the credentials of your source portal

#Block 2: Exporting Hosted Feature Layer from AGOL
item_id='f8f66537f7504bc1b2bd4585e83c13ec' #replace with the service_ID of the hosted feature layer to download
GDBname = "Temp_FGB" #name of the temporary GDB to be saved in ArcGIS Online

AGOLitem = gis.content.get(item_id)

print ("Exporting Hosted Feature Layer...")
AGOLitem.export(GDBname,'File Geodatabase', parameters=None, wait='True')
time.sleep(10)#add 10 seconds delay to allow export to complete

search_fgb = gis.content.search(query = "title:{}*".format(GDBname )) #find the newly created file geodatabase in ArcGIS online
fgb_item_id = search_fgb[0].id
fgb = gis.content.get(fgb_item_id)
fgb.download(save_path=outputFolder) #downloads the fgdb on the path you selected

#Block 3: Reconnecting to ArcGIS Online
from arcgis.gis import GIS
import getpass
username = input("Input your username: ")
gis = GIS("https://esp-esri-co.maps.arcgis.com", username) #replace these with the credentials of your target portal

#Block 4: Uploading GDB to ArcGIS Online
import os
from arcgis.gis import GIS
data_path = (r"C:\Users\vurrego\Documents\Test_fGDB\Temp_FGB.zip") #Where the fgdb is saved
zip_properties = {'type':'File Geodatabase'} #type of the document you want to upload
add_fgdb = gis.content.add(item_properties = zip_properties, data = data_path) #Adds the fgdb on your portal content
print("The fGDB is uploaded")

#Block 5: Sharing the GDB Item
add_fgdb.share(groups="32cc4d232e45464d8c536a076c2f0d70") #replace with the id of the group you want to share

 

 

 

I hope this will be useful for you! 

Have a great day.