Select to view content in your preferred language

Overwrite Web Layer

383
5
Jump to solution
03-15-2025 10:31 PM
ClintBoaz1
Occasional Contributor

When trying to overwrite a web layer with a feature in a GDB via a python script I get the error bellow

-- ValueError: The data file provided does not exist or could not be accessed.  --  

The problem matches BUG-000172631 although Esri has that error as Status: Non-Reproducible.

Anyone use this or a similar workflow and have any advice?  Thanks!  

 

 

 

import arcpy
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

arcpy.env.overwriteOutput = True

# Variables
itemid_roads = '4377ec12e886476ab96ce71ce59d3f60'

#Log into AGOL
gis = GIS('pro')
me = gis.users.me
print ("Signed in as: " + str(me))

#Overwrite Hosted Feature Layer
OutputGDB = "//nwshfp01/lidar/geodb/agol_features.gdb/"
newname = (OutputGDB + "Roads_CL_")
dataitem = gis.content.get(itemid_roads)
flayercol = FeatureLayerCollection.fromitem(dataitem)
flayercol.manager.overwrite(newname)

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @ClintBoaz1,

Does either of the below suit your workflows? 

@DonMorrison1 is correct, where/how the original is published can affect overwriting. 

ArcPy

You can publish from ArcGIS Pro manually or programmatically. Check out my blog post here for publishing with ArcPy. When overwriting you just need to add in the code on line 77 below. Even if you initially publish manually from Pro, you can still use Python (ArcPy) to overwrite.

 

import arcpy
import os

################################################################################
## Esri Documentation:
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/stage-service.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/upload-service-definition.htm
##
##
## ArcGIS Pro Version 3.4.0
##
################################################################################

################################################################################
## INPUT REQUIRED  #############################################################

## the path to the APRX that contains the Map with the layers to publish
aprx_path = r"C:\path\to\project\your_project.aprx"

## the name of the Map in the APRX that contains the layers to publish
map_name = "NAME OF MAP"

## a folder to stage the definition files
staging_fldr = r"C:\path\to\staging_folder"

## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"

## a description for our Hosted Feature Service
description = "This is our description"

## a summary for our Hosted Feature Service
summary = "This is our summary"

################################################################################
## ACCESS ARCGIS PRO COMPONENTS  ###############################################

## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the correct map
m = aprx.listMaps(map_name)[0]

################################################################################
## STAGING #####################################################################

## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldr, f"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldr, f"{map_name}.sd")

## if either of the output files already exists, then delete
if arcpy.Exists(sddraft_output_filepath):
    arcpy.Delete_management(sddraft_output_filepath)

if arcpy.Exists(sd_output_filepath):
    arcpy.Delete_management(sd_output_filepath)

################################################################################
## MANIPULATE THE SD DRAFT ######################################################

## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
    server_type = "HOSTING_SERVER",
    service_type = "FEATURE",
    service_name = map_name
)

## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags
sd_draft.allowExporting = True
sd_draft.overwriteExistingService = True # OVERWRITE

## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)

################################################################################
## STAGE THE SERVICE ###########################################################

## stage the service
arcpy.server.StageService(
    in_service_definition_draft = sddraft_output_filepath,
    out_service_definition = sd_output_filepath
)

################################################################################
## PUBLISH TO ARGIS ONLINE #####################################################

## publish to agol
arcpy.server.UploadServiceDefinition(
    in_sd_file = sd_output_filepath,
    in_server = "HOSTING_SERVER"
)

################################################################################
print("\nSCRIPT COMPLETE")

 

There are more advanced workflows for publishing and overwriting that digs into the xml for various setting, you can find these in the documentation links at the top of the script. The above is really a minimum required to publish/overwrite. 

 

ArcGIS API for Python

Publish an original Feature Service based on the File Geodatabase.

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

## Path to File Geodatabase Zipped
gdb = r"C:\path\to\zipped_gdb.zip"

## Properties
gdb_properties = {"title" : "PUBLISHED_FROM_API", "type" : "File Geodatabase"}

## Access ArcGIS Online
agol = GIS("home")

## Add File Geodatabase to AGOL
gdb_item = agol.content.add(item_properties=gdb_properties, data=gdb)

## Publish as new feature service
new_fs = gdb_item.publish()

print(new_fs.id)

## you can delete the File Geodatabase added to AGOL

 

Overwrite... 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

## Path to File Geodatabase Zipped with data to use to overwrite
gdb = r"C:\path\to\zipped_gdb.zip"

## Access ArcGIS Online
agol = GIS("home")

## Fature Service as an Item object - the one to overwrite
item = agol.content.get("ITEM_ID")

## Overwrite
flc = FeatureLayerCollection.fromitem(item)
print(flc.manager.overwrite(gdb))

 

Note! The name of the file geodatabase and the feature class(es) must be the same as the original that was published or else you will get 

ValueError: The name and extension of the file must be the same as the original data.
 
I hope that helps.
All the best,
Glen
~ learn.finaldraftmapping.com

View solution in original post

5 Replies
TonyAlmeida
MVP Regular Contributor

Try to check to make sure the feature exists,

# Verify the feature class exists
if not arcpy.Exists(newname):
    print(f"Error: The feature class {newname} does not exist or is inaccessible.")
else:
    print(f"Feature class {newname} exists.")

Also try

OutputGDB = r"//nwshfp01/lidar/geodb/agol_features.gdb"
newname = f"{OutputGDB}/Roads_CL"

 

Maybe try saving the feature class as a ZIP shapefile and try uploading

 

ClintBoaz1
Occasional Contributor

Thanks Tony, it did exist, it might have been an issue with me originally publishing from ArcGIS Pro and not through python.  I ended up going down the feature service draft/definition route 

0 Kudos
DonMorrison1
Frequent Contributor

I have had problems also trying to figure out how to programatically overwrite hosted layers too. How did you originally publish the hosted feature layer? According to this page, it looks like if you published it from ArcGIS Pro, then you can only overwrite it from Pro (I suspect that does not include using the python window in Pro).  

"If you published the hosted feature layer from ArcGIS Pro, you must overwrite the service from ArcGIS Pro."

I ran a test using your script and got the same Value Error message (I originally published from layer from ArcGIS Pro). But when I loaded the new data into a ArcGIS Pro map then did Share -> Web Layer -> Overwrite Web Layer it worked. 

Clubdebambos
MVP Regular Contributor

Hi @ClintBoaz1,

Does either of the below suit your workflows? 

@DonMorrison1 is correct, where/how the original is published can affect overwriting. 

ArcPy

You can publish from ArcGIS Pro manually or programmatically. Check out my blog post here for publishing with ArcPy. When overwriting you just need to add in the code on line 77 below. Even if you initially publish manually from Pro, you can still use Python (ArcPy) to overwrite.

 

import arcpy
import os

################################################################################
## Esri Documentation:
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/stage-service.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/upload-service-definition.htm
##
##
## ArcGIS Pro Version 3.4.0
##
################################################################################

################################################################################
## INPUT REQUIRED  #############################################################

## the path to the APRX that contains the Map with the layers to publish
aprx_path = r"C:\path\to\project\your_project.aprx"

## the name of the Map in the APRX that contains the layers to publish
map_name = "NAME OF MAP"

## a folder to stage the definition files
staging_fldr = r"C:\path\to\staging_folder"

## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"

## a description for our Hosted Feature Service
description = "This is our description"

## a summary for our Hosted Feature Service
summary = "This is our summary"

################################################################################
## ACCESS ARCGIS PRO COMPONENTS  ###############################################

## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the correct map
m = aprx.listMaps(map_name)[0]

################################################################################
## STAGING #####################################################################

## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldr, f"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldr, f"{map_name}.sd")

## if either of the output files already exists, then delete
if arcpy.Exists(sddraft_output_filepath):
    arcpy.Delete_management(sddraft_output_filepath)

if arcpy.Exists(sd_output_filepath):
    arcpy.Delete_management(sd_output_filepath)

################################################################################
## MANIPULATE THE SD DRAFT ######################################################

## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
    server_type = "HOSTING_SERVER",
    service_type = "FEATURE",
    service_name = map_name
)

## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags
sd_draft.allowExporting = True
sd_draft.overwriteExistingService = True # OVERWRITE

## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)

################################################################################
## STAGE THE SERVICE ###########################################################

## stage the service
arcpy.server.StageService(
    in_service_definition_draft = sddraft_output_filepath,
    out_service_definition = sd_output_filepath
)

################################################################################
## PUBLISH TO ARGIS ONLINE #####################################################

## publish to agol
arcpy.server.UploadServiceDefinition(
    in_sd_file = sd_output_filepath,
    in_server = "HOSTING_SERVER"
)

################################################################################
print("\nSCRIPT COMPLETE")

 

There are more advanced workflows for publishing and overwriting that digs into the xml for various setting, you can find these in the documentation links at the top of the script. The above is really a minimum required to publish/overwrite. 

 

ArcGIS API for Python

Publish an original Feature Service based on the File Geodatabase.

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

## Path to File Geodatabase Zipped
gdb = r"C:\path\to\zipped_gdb.zip"

## Properties
gdb_properties = {"title" : "PUBLISHED_FROM_API", "type" : "File Geodatabase"}

## Access ArcGIS Online
agol = GIS("home")

## Add File Geodatabase to AGOL
gdb_item = agol.content.add(item_properties=gdb_properties, data=gdb)

## Publish as new feature service
new_fs = gdb_item.publish()

print(new_fs.id)

## you can delete the File Geodatabase added to AGOL

 

Overwrite... 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

## Path to File Geodatabase Zipped with data to use to overwrite
gdb = r"C:\path\to\zipped_gdb.zip"

## Access ArcGIS Online
agol = GIS("home")

## Fature Service as an Item object - the one to overwrite
item = agol.content.get("ITEM_ID")

## Overwrite
flc = FeatureLayerCollection.fromitem(item)
print(flc.manager.overwrite(gdb))

 

Note! The name of the file geodatabase and the feature class(es) must be the same as the original that was published or else you will get 

ValueError: The name and extension of the file must be the same as the original data.
 
I hope that helps.
All the best,
Glen
~ learn.finaldraftmapping.com
ClintBoaz1
Occasional Contributor

Thanks Clubdebambos, this is the route I went down and finally got it to work.  I really appreciate your in-depth answer.

I am definitely a python beginner but it seems silly it takes this many lines of code to do something relatively basic (through Pro at least) that I assume most all organizations that host data online have to do.  

0 Kudos