Select to view content in your preferred language

Publishing web layer in bulk

2399
3
07-04-2019 08:30 PM
AkiyoshiKawamura1
Emerging Contributor

Hi,

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

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

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 = arcpy.mp.ArcGISProject(aprxFile)
for m in aprx.listMaps('*'):

    print("Map: " + m.name)
    serverFolder = m.name

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

    for lyr in m.listLayers():
        service_name = lyr.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

        sharing_draft.exportToSDDraft(sddraft_filename)

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\sharing.py", 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??

Thanks.

Tags (2)
0 Kudos
3 Replies
JohnFix2
Regular Contributor

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 "sharing.py" 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\sharing.py", 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
AkiyoshiKawamura1
Emerging Contributor

Below code works for me.

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

federatedServerUrl = "Url to ArcGIS server"
aprxProj = arcpy.mp.ArcGISProject("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
sharing_draft.exportToSDDraft(sddraft_filename)
# 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 typeName.firstChild.data == "FeatureServer":
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Enable Feature Access.
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = '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')
doc.writexml(f)
f.close()

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

# Publish
arcpy.UploadServiceDefinition_server(
in_sd_file=sd_filename,
    in_server="AGS file to ArcGIS server",
in_service_name=service_name,
in_startupType="STARTED")
0 Kudos
José_ManuelMendoza
Emerging Contributor

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 = arcpy.mp.ArcGISProject(r"C:\ArcgisData\Webmapdata\RA_visualisation.aprx")
aprx_template.saveACopy(r"C:\ArcgisData\RA_visualisation\Temp_projects\{}.aprx".format(service))
log("created aprx")
aprx = arcpy.mp.ArcGISProject(r"C:\ArcgisData\RA_visualisation\Temp_projects\{}.aprx".format(service))
m = aprx.listMaps("Map")[0]
layers_in_current = m.listLayers()
layers_in_current.append(layer_tmp)


m.addDataFromPath(layer_tmp[0])
log("Layer added to map")
aprx.save()


# 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:

Jos_ManuelMendoza_0-1634743747277.png

The weird part is that the tool is working locally.

 

Any hint? 

 

Thanks a lot

0 Kudos