-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.
-I have been able to copy the layer and publish it to portal using to_featurelayer()
The issue is that the layer will not overwrite the existing layer in portal.
Here is the code I have been trying:
def overwrite_existing_layer(source_layer_name,feature_service_id,copy_layer_name):
try:
# Connect to the ArcGIS Portal
gis = GIS('pro')
# 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
temporary_copy = source_layer.layers[0].query()
# Creat Pandas df from temporary copy
temp_sdf = temporary_copy.sdf
# Create a dictionary for the properties of to_featurelayer method
fl_properties = {
'layer': 0,
'featureServiceId': feature_service_id
}
# Overwrite the feature layer
new_layer = temp_sdf.spatial.to_featurelayer(title=copy_layer_name, overwrite=True, service={'layer': 0, 'featureServiceId': feature_service_id})
print("Temporary copy overwritten in SQL database.")
print('Finished running script')
except Exception as e:
# Handle any errors that occur during the process
print(f"An error occurred: {str(e)}")
The script runs fine but does not overwrite the existing layer. I have the correct feature_service_id from the existing layer in portal but am not sure why it does not recognize the existing layer.
Normally in Pro there is an "overwriteOutputs" option" to enable you do do exactly what you want. I dont' know if it can be used with the api and portal.
You might want to "delete" using the appropriate python arcgis api call prior to adding the newer version.
Dan, I appreciate your response. Unfortunately have not found a way to overwrite like you can in pro.