ArcGIS Python – Upload Layer from LYR File to AGOL with Symbology

feature-layerpythonsymbology

I have a script which creates a new shapefile from a text file, and I symbolize it by type from another template layer. This allows me to toss the shapefile into ArcGIS Pro with all the proper symbologies.

Now, within ArcGIS Pro, I can right click the layer and go to "Share as Web Layer".

Is there a specific Python syntax within ArcPy to allow that?

My search results have given less than ideal methods.

TL;DR I have a .lyrx file with proper symbology I would like to upload to AGOL from within a standalone Python script.

Edit: I've found a solution which creates a symbolized layerx file, adds it to a new .aprx, and uploads a service definition, which, by nature, creates a Hosted Feature Service of the symbolized layer I wanted uploaded.

# Activate new copied project
aprx = arcpy.mp.ArcGISProject(ArcGISProject.aprx)

m = aprx.listMaps()
m = m[0]

# Add Symbolized layer to project file
m.addDataFromPath(layerX)

# Save the project
aprx.saveACopy(outAprxPath)

# Activate newly copied map (copying might be redundant)
aprx2 = arcpy.mp.ArcGISProject(outAprx)
m = aprx2.listMaps()
m = m[0]

# Begin FeatureSharingDraft testing

outdir = enterFolderPathHere
service_name = proj_name + "_pnts"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference Map to Publish
# Create FeatureSharingDraft and set metadata, portal folder, and export data properties
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.credits = "These are credits"
sddraft.description = "This is description"
sddraft.summary = "This is a summary"
sddraft.tags = "Tag, Tag2, Tag3"
sddraft.useLimitations = "These are use limitations"
sddraft.portalFolder = "Folder to be added to / Created Here"
sddraft.allowExporting = True

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")

Best Answer

This solution below takes a created .lyrx file with the symbology I want, adds it to a new .aprx, and uploads a service definition, which, by nature, creates a Hosted Feature Service of the symbolized layer I wanted uploaded.

# Activate new copied project
aprx = arcpy.mp.ArcGISProject(ArcGISProject.aprx)

m = aprx.listMaps()
m = m[0]

# Add Symbolized layer to project file
m.addDataFromPath(layerX)

# Save the project
aprx.saveACopy(outAprxPath)

# Activate newly copied map (copying might be redundant)
aprx2 = arcpy.mp.ArcGISProject(outAprx)
m = aprx2.listMaps()
m = m[0]

# Begin FeatureSharingDraft testing

outdir = enterFolderPathHere
service_name = proj_name + "_pnts"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference Map to Publish
# Create FeatureSharingDraft and set metadata, portal folder, and export data properties
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.credits = "These are credits"
sddraft.description = "This is description"
sddraft.summary = "This is a summary"
sddraft.tags = "Tag, Tag2, Tag3"
sddraft.useLimitations = "These are use limitations"
sddraft.portalFolder = "Folder to be added to / Created Here"
sddraft.allowExporting = True

# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)

print("Finish Publishing")

There is most likely a more elegant method but this worked for me.

Related Question