-Goal: I am trying to create a script to access a layer on portal and clone it. After cloning it I would like to overwrite a layer in either a SQL database or back in portal.
-Reason: As the original layer updates I would like to run the script to update the copy in which I have view layers created from.
-What I have tried: I have tried a multitude of things, but have not found a way to clone the layer without hitting a type error or a way to save the layer in a SQL DB or portal.
-Here is some code and errors for creating a clone:
source_layer = gis.content.search(source_layer_name, item_type="Feature Layer")[0]
temporary_copy = ContentManager.clone_items(source_layer)
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: clone_items() missing 1 required positional argument: 'items'
temporary_copy = ContentManager.clone_items(items=source_layer)
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: clone_items() missing 1 required positional argument: 'self'
-For saving the layer I have really only tried publish(), but it doesn't seem to take a layer as an input.
How do you save a layer in portal? in a SQL DB? using arcgis api for python?
-Here is what the entire script I have been working with:
def overwrite_existing_layer(source_layer_name, sql_database, layer_name):
try:
# Connect to the ArcGIS Portal
gis = GIS('pro')
portal_dstore = gis.datastore
# Retrieve the feature layer you want to clone
source_layer = gis.content.search(source_layer_name, item_type="Feature Layer")[0]
# Create a temporary copy of the layer or use clone_item() shown above
temporary_copy = source_layer.layers[0].query()
# Save the temporary copy to overwrite the existing layer in the SQL database
temporary_copy.publish()
print("Temporary copy overwritten in SQL database.")
# Delete the temporary copy
# portal_dstore.delete_layers(temporary_copy)
# if not temporary_copy.exists:
# print("Temporary copy deleted.")
print('Finished running script')
except Exception as e:
# Handle any errors that occur during the process
print(f"An error occurred: {str(e)}")
-Any and all advice is appreciated. I'm pretty new to using python within the ArcGIS suite. I have not found the esri documentation very useful so if there is any other recommended resources, those would be appreciated!