0

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")
5
  • Have you looked at arcpy.sharing?
    – PolyGeo
    Commented Jul 18, 2022 at 20:09
  • @PolyGeo Arcpy.Sharing has been looked at. FeatureSharingDraft has given me the most luck, but wants to share an entire Map from an .aprx file and upload that. It could work as it does result in a new Hosted Feature Service with my symbolized layer, but that adds quite a few steps to what I imagine would be a simple automation.
    – Barnard87
    Commented Jul 18, 2022 at 20:34
  • Since you've made a code attempt, I think you should present it so that the community becomes likely to re-open your question.
    – PolyGeo
    Commented Jul 18, 2022 at 20:41
  • You should look at the ArcGIS API for Python (a.k.a. the arcgis package, which was designed to work with AGOL in a much more efficient way than arcpy. This page shows the general workflow, although not for a layer package specifically.
    – nmpeterson
    Commented Jul 26, 2022 at 14:31
  • @nmpeterson I've actually solved it with the Service Definition upload method but I'll definitely take a look into that as I do a lot of AGOL automations. Cheers!
    – Barnard87
    Commented Jul 26, 2022 at 15:37

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.