Publishing web layer in bulk

07-04-2019 08:30 PM
New Contributor


I am implementing a script to publish web layer in bulk into Enterprise ArcGIS Server v10.7.0 based on below example.

What I am trying is to create a Pro project file which has several maps having multiple feature classes sourced from EGDB, create a script to get all maps and layers in the project file, and publish each of layers as a web layer by reference. Our ArcGIS server is federated.

import arcpy
from arcpy import env
import os

aprxFile = r"pathto\Bulk_Layer_Publishing.aprx"

outDir = r"pathto\Output_sd"

# Reference map to publish
aprx =
for m in aprx.listMaps('*'):

    print("Map: " +
    serverFolder =

    serviceDirName = "{}\\{}".format(outDir,
    if not os.path.exists(serviceDirName):

    for lyr in m.listLayers():
        service_name =

        sddraft_filename = "{}\\{}".format(serviceDirName, service_name + ".sddraft")
        # Create MapServiceDraft and set service properties
        sharing_draft = arcpy.sharing.CreateSharingDraft('STANDALONE_SERVER', 'MAP_SERVICE', service_name, lyr)
        targetServer = r"pathTo\MyArcGIS.ags"
        sharing_draft.targetServer = targetServer
        sharing_draft.serverFolder = serverFolder # same as Map name
       sharing_draft.copyDataToServer = False


Error: Target server is not a standalone server or is inaccessible.

Traceback (most recent call last):
File "<string>", line 45, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\", line 91, in exportToSDDraft
return _convertArcObjectToPythonObject(self._arc_object.exportToSDDraft(out_sddraft,self))
ValueError: Target server is not a standalone server or is inaccessible.

Could anyone advise me what's wrong with my code??


Tags (2)
0 Kudos
3 Replies
New Contributor III

I get this error when I'm trying to create an SD Draft file in ArcGIS Pro. I want to make an .SD file that I can then publish to our ArcGIS Server 10.6.1.

I believe I have created a valid .ags file. I can use the paramaters I specify in the .ags file to open the admin page of that server. But it IS a stand alone ArcGIS Server....NOT a federated server. 

I get an error in "" at line 91. Here are the lines in that block.....does this indicate this only works for federated servers?

def exportToSDDraft(self, out_sddraft):
self.federatedServerUrl = self.targetServer # todo: temp fix
return _convertArcObjectToPythonObject(self._arc_object.exportToSDDraft(out_sddraft,self))

Here is the error I get in ArcGIS Pro python windows:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\", line 91, in exportToSDDraft
return _convertArcObjectToPythonObject(self._arc_object.exportToSDDraft(out_sddraft,self))
ValueError: Target server is not a standalone server or is inaccessible.

So, I'm basically dead in the water until I can figure out what is going on....

0 Kudos
New Contributor

Below code works for me.

I repeated this process to publish around 500+ services in bulk.

federatedServerUrl = "Url to ArcGIS server"
aprxProj ="CURRENT")
aprxMap = aprxProj.listMaps("Map")[0]

service_name = "Service_Name"
serviceFolder = "Service_Folder"
sddraft_filename = "{}\\{}".format(serviceFolder, service_name + ".sddraft")
sharing_draft = aprxMap.getWebLayerSharingDraft("FEDERATED_SERVER", "MAP_IMAGE", service_name)
sharing_draft.federatedServerUrl = federatedServerUrl
sharing_draft.copyDataToServer = False
# sharing_draft.offline = False
sharing_draft.overwriteExistingService = False # True
sharing_draft.serverFolder = serviceFolder
sharing_draft.summary = summary
sharing_draft.tags = tags
sharing_draft.description = description
#sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

# Create Service Definition Draft file
# Read the sddraft xml.
doc = DOM.parse(sddraft_filename)

# Find all elements named TypeName. This is where the server object extension
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
    # Get the TypeName we want to enable.
    if == "FeatureServer":
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Enable Feature Access.
            if extElement.tagName == 'Enabled':
       = 'true'
# Output to a new sddraft with Feature service enabled
sddraft_mod_xml = service_name + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file  = "{}\\{}".format(serviceFolder, sddraft_mod_xml)

f = open(sddraft_mod_xml_file, 'w')

# Stage Service
sd_filename = "{}\\{}".format(serviceFolder, service_name + ".sd")
arcpy.StageService_server(sddraft_mod_xml_file, sd_filename)

# Publish
    in_server="AGS file to ArcGIS server",
0 Kudos
New Contributor II

Hi @AkiyoshiKawamura1 ,

I have seen that you have solved this issue. I have a similar problem, I cannot publish my script as a web tool in my  Enterprise, because the .sddraft file is not being created, here is part of my script:

outdir = r"C:\ArcgisData\Webmapdata\{}".format(CHnumber)
service = CHnumber
sddraft_filename = service + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)

RiskAssessment_output = r'C:\ArcgisData\Webmapdata\{}\{}'.format(CHnumber,CHnumber)
#DeforestationRisk_points_lyrx = "C:\\ArcgisData\\RA_visualisation\\Symbology\\polygon.lyrx"

# Reference map to publish

aprx_template ="C:\ArcgisData\Webmapdata\RA_visualisation.aprx")
log("created aprx")
aprx ="C:\ArcgisData\RA_visualisation\Temp_projects\{}.aprx".format(service))
m = aprx.listMaps("Map")[0]
layers_in_current = m.listLayers()

log("Layer added to map")

# Create TileSharingDraft and set service properties
sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service)
log("sddraft properties defined")
sharing_draft.overwriteExistingService = True
sharing_draft.summary = "Testing publishing of layers"
sharing_draft.tags = "risk map"
sharing_draft.description = "My Description"
sharing_draft.credits = "My Credits"
sharing_draft.useLimitations = "My Use Limitations"

# Create Service Definition Draft file
log("start creating sddraft file")
sharing_draft.exportToSDDraft(sddraft_output_filename) <- IT IS FAILING IN THIS LINE
log("sddraft created")
# Stage Service
sd_filename = service + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
log("Staging service")
arcpy.StageService_server(sddraft_output_filename, sd_output_filename)

 This is the error I always get:


The weird part is that the tool is working locally.


Any hint? 


Thanks a lot

0 Kudos