Select to view content in your preferred language

Overwrite Feature Layer from ArcGIS Pro to ArcGIS Online using arcpy

1019
4
Jump to solution
01-19-2022 05:20 AM
Labels (1)
Alice
by
Emerging Contributor

Hello everybody!

I would like to overwrite several layers from ArcGIS Pro to ArcGIS Online using arcpy. The layer names in ArcGIS Online and Pro are completly different from each other.

My idea was to assign the new layer in ArcGIS Pro with the old layer in ArcGIS Online via a dictionary. But I dont know how to proceed...

Could you please help me with that?

I'm very new to scripting. I used a lot from https://github.com/rooschu/overwrite-web-layers/blob/master/OverwriteWebLayers.py

and

 

https://pro.arcgis.com/en/pro-app/2.8/arcpy/sharing/featuresharingdraft-class.htm

 

My script so far is:

 

 
prjFolder = r"path"
prjPath = os.path.join(prjFolder, r"path to .aprx")

# Set login credentials 
portal = "..."  
user = "XXX"
password = "XXX"

# Assign name and location for temporary staging files
tempPath = prjFolder

#select layers in arcgis pro
layers = geoMap.listLayers()
service_name = []
for x in layers:
    service_name.append(x.name)

#sharing draft
stringdraft = ".sddraft"
sddraft_filename = [x + stringdraft for x in service_name]
sddraft = os.path.join(tempPath, *sddraft_filename)
#sharing sd
stringsd = ".sd"
sd_filename = [x + stringsd for x in service_name]
sd = os.path.join(tempPath, *sd_filename)

# Connect to ArcGIS online
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
print("Successfully logged in as: " + gis.properties.user.username + "\n")

# Assign environment and project, and create empty dictionaries
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
layerDict = {}
servDict = {}

# Populate map dictionary with map names and objects from earlier defined project
for layer in geoMap.listLayers ():
    layerDict[layer.name] = layer


#select ArcGIS Online Web Layers
lyItems = gis.content.search(query="owner:{0}".format(user),item_type="Feature Layer", max_items=100)
ext= ["WFL1", "_Faelle", "genschaft"]
service_name = [item for item in lyItems if item.title.endswith(tuple(ext))]

 

Also when I try to create the both os.path.join() it doesn't create six diferent paths but one very long path with all my layers added.

 

 

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

As I suspected.  By example compare your results to this example

tempPath = r"c:\folder"
tempPath
'c:\\folder'

service_name = ['a', 'b', 'c', 'd']
stringsd = ".sd"
sd_filename = [x + stringsd for x in service_name]
sd = os.path.join(tempPath, *sd_filename)
sd
'c:\\folder\\a.sd\\b.sd\\c.sd\\d.sd'  # -- your output

for nme in sd_filename:
    sd = os.path.join(tempPath, nme)
    print(sd)
# --- my output
c:\folder\a.sd
c:\folder\b.sd
c:\folder\c.sd
c:\folder\d.sd

... sort of retired...

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

would help to provide line numbers and code syntax highlights

you collect values, then joining all of them, so you have an indentation issue, format the code and see if the join is occurring inside or outside the collection loop


... sort of retired...
0 Kudos
Alice
by
Emerging Contributor

thanks for the replay. I changed the code

0 Kudos
DanPatterson
MVP Esteemed Contributor

As I suspected.  By example compare your results to this example

tempPath = r"c:\folder"
tempPath
'c:\\folder'

service_name = ['a', 'b', 'c', 'd']
stringsd = ".sd"
sd_filename = [x + stringsd for x in service_name]
sd = os.path.join(tempPath, *sd_filename)
sd
'c:\\folder\\a.sd\\b.sd\\c.sd\\d.sd'  # -- your output

for nme in sd_filename:
    sd = os.path.join(tempPath, nme)
    print(sd)
# --- my output
c:\folder\a.sd
c:\folder\b.sd
c:\folder\c.sd
c:\folder\d.sd

... sort of retired...
0 Kudos
Alice
by
Emerging Contributor

Thank you very much! It works! 😄

0 Kudos