I need a complete workflow for publishing layers or layer collections in Python.

293
3
Jump to solution
05-07-2021 11:38 AM
BrianWilson7
Occasional Contributor II

I keep finding examples on how to publish services using existing SD files but not a complete workflow from defining a feature layer or collection of feature layers to be published, creating the SDDraft, and then uploading and publishing it.

Step 1, create an sddraft file.

Step 2, Use "Stage Service" to turn an .sddraft into an .sd

Step 3, Use "Upload Service Definition" to upload and publish it.

Step 1 is missing. I found "CreateMapSDDraft" but this reads an MXD to publish a map and I want to publish feature services and I no longer use ArcMap or MXD files.

When I author in ArcPro, it creates the file, stages it, uploads it, and deletes it. I find this annoying, I'd like to at least get a copy of the sddraft so I can republish later under control of a Python script. I'd like to be able to create and maintain sddraft files without monkeying around in the ArcPro GUI. 

 

0 Kudos
1 Solution

Accepted Solutions
DavidColey
Frequent Contributor

Brian - In addition to what Michael has posted, everything you need to get started is contained in this sample:

https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm#C_GUID-4EAB8A36...

Or you can go here:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/createweblayersddraft.htm#C_GUID-AAC21E52-6C6...

and look at the last sample in the list. 

For either one, you could put them in a conditional like this:

    for m in aprx.listMaps("AppraiserLayers"):
        print("Map: " + m.name)
        for lyr in m.listLayers():
            if  lyr.name == 'PropertySalesPublic':
                lyrList = []
                lyrList.append(m.listLayers(lyr.name)[0])
                print(lyr.name)
                stageSDDraft(m, lyr.name, "snippit", lyrList, "descript","Portal Folder","tags","sd folder on your drive")
                print(lyr.name + " Draft Created")
                arcpy.StageService_server('C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sddraft', 'C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sd')
                print(lyr.name + " Service Staged")
                arcpy.UploadServiceDefinition_server('C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sd', 'My Hosted Services', "","","","","","OVERRIDE_DEFINITION","SHARE_ONLINE","PUBLIC","SHARE_ORGANIZATION","")
                print(lyr.name + " Service Uploaded")
                message = message + "\n" + "1. " + (lyr.name) + " Service Uploaded"

 

but I still like to keep the old blog that Michael posted in his reply handy because its helpful to see how to use both arcpy and the arcgis api together

View solution in original post

3 Replies
BrianWilson7
Occasional Contributor II

I have not, that looks like a good lead. THANKS

In the meantime I found the "save as sd" option in ArcPro, created an SD, unpacked it, and threw my hands up in the air as it's another complex nest of uglified XML and JSON files. Still, better than the old days of binary files like MXDs!!

0 Kudos
DavidColey
Frequent Contributor

Brian - In addition to what Michael has posted, everything you need to get started is contained in this sample:

https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm#C_GUID-4EAB8A36...

Or you can go here:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/createweblayersddraft.htm#C_GUID-AAC21E52-6C6...

and look at the last sample in the list. 

For either one, you could put them in a conditional like this:

    for m in aprx.listMaps("AppraiserLayers"):
        print("Map: " + m.name)
        for lyr in m.listLayers():
            if  lyr.name == 'PropertySalesPublic':
                lyrList = []
                lyrList.append(m.listLayers(lyr.name)[0])
                print(lyr.name)
                stageSDDraft(m, lyr.name, "snippit", lyrList, "descript","Portal Folder","tags","sd folder on your drive")
                print(lyr.name + " Draft Created")
                arcpy.StageService_server('C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sddraft', 'C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sd')
                print(lyr.name + " Service Staged")
                arcpy.UploadServiceDefinition_server('C:/ArcProProjects/PortalUpdates/Appraiser/' + lyr.name + '.sd', 'My Hosted Services', "","","","","","OVERRIDE_DEFINITION","SHARE_ONLINE","PUBLIC","SHARE_ORGANIZATION","")
                print(lyr.name + " Service Uploaded")
                message = message + "\n" + "1. " + (lyr.name) + " Service Uploaded"

 

but I still like to keep the old blog that Michael posted in his reply handy because its helpful to see how to use both arcpy and the arcgis api together

View solution in original post