Using python to publish a map service

13562
7
Jump to solution
06-04-2015 06:54 AM
NolanAlbarelli
Occasional Contributor

Can someone help me with a python script to publish a feature layer as a map service (overwrite map/feature service).

0 Kudos
1 Solution

Accepted Solutions
PrevinWong1
Esri Contributor

esri has a solution called ArcREST.  A python package that handles all the publishing details to ArcGIS Server, Portal, and ArcGIS online.  Give it a look here.  You configure a json file and execute it via python.

Esri/ArcREST · GitHub

View solution in original post

7 Replies
JakeSkinner
Esri Esteemed Contributor
MicahBabinski
Frequent Contributor

Greetings Nolan,

A couple questions for you:

1. Does the service you'd like to overwrite have "Feature Access" enabled? In other words, does it need to be editable?

2. Is the service you'd like to overwrite published on your own on-premise ArcGIS Server, or on Esri's "My Hosted Services?"

For an on-premise map service that does not require feature access, the following script (set up for a script tool but could be modified to be run stand-alone outside of ArcGIS), should get you there:

# overwrite_service.py
# Created: 3/11/2015
# Author:   Micah Babinski
#           City of Portland
#           Bureau of Technology Services
#           micah.babinski@portlandoregon.gov
#           503.823.1091
#
# Description: Overwrites an existing map service on ArcGIS Server
#


import arcpy
import xml.dom.minidom as DOM


# User parameters
map_document = arcpy.GetParameterAsText(0)
service_folder = arcpy.GetParameterAsText(1)
service_name = arcpy.GetParameterAsText(2)


# Create the MXD object and define the summary and tags
arcpy.AddMessage("Creating an MXD object and retrieving the summary and tags.")
mxd = arcpy.mapping.MapDocument(map_document)
summary = mxd.summary
tags = mxd.tags


# Define the workspace path
arcpy.AddMessage("Defining the workspace path.")
workspace = arcpy.Describe(map_document).path + "\\"


# Define the AGS connection
arcpy.AddMessage("Defining the AGS connection file.")
con = #r"Path to your arcgis server connection eg: GIS Servers\arcgis on myserver_6080 (admin)"


# Create a variable representing the output service definition draft
sddraft = workspace + service_name + '.sddraft'


# Create a variable representing the output service draft
sd = workspace + service_name + '.sd'


# Create service definition draft
arcpy.AddMessage("Creating the service definition draft.")
analysis = arcpy.mapping.CreateMapSDDraft(mxd, sddraft, service_name, 'ARCGIS_SERVER', con, False, service_folder, summary, tags)


# Set service type to esriServiceDefinitionType_Replacement
arcpy.AddMessage("Setting the service type to be a replacement.")
newType = 'esriServiceDefinitionType_Replacement'
xml = sddraft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
    if desc.parentNode.tagName == 'SVCManifest':
        if desc.hasChildNodes():
            desc.firstChild.data = newType
outXml = xml    
f = open(outXml, 'w')     
doc.writexml( f )     
f.close()


# Stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
    # Execute StageService
    arcpy.AddMessage("Staging the service.")
    arcpy.StageService_server(sddraft, sd)
    # Execute UploadServiceDefinition
    arcpy.AddMessage("Uploading the service.")
    arcpy.UploadServiceDefinition_server(sd, con)
    arcpy.AddMessage("Upload successful!")
else: 
    # If the sddraft analysis contained errors, display them
    arcpy.AddMessage("The following errors prevented overwriting of the service:")
    arcpy.AddMessage(analysis['errors'])


# Remove the sd draft and sd files
arcpy.AddMessage("Cleaning up.")
arcpy.Delete_management(sd)
arcpy.Delete_management(sddraft)

Now if you need feature access, it gets more complicated . I haven't figured out how to overwrite an on-premise editable feature service with Python yet, but I have asked about it on geonet. For overwriting an Esri-hosted feature service, there is a code sample here, but I haven't tested it yet.

If you figure out how to overwrite the on-premise editable service, PLEASE let me know. Happy scripting!

Micah

ToddHenry1
Deactivated User

I have the same issue and the code snipped I'm using to enable feature access is the same as Micah and comes directly from the ESRI help files.

0 Kudos
PhilippNagel
Emerging Contributor

Hi Micah,

Thanks for sharing that script. I did change it to be used as a stand-alone script, it works like a charm! The one shown in the SA post does not seem to be very robust and your's is very elegant!

0 Kudos
MicahBabinski
Frequent Contributor

Cheers, Philipp! Happy publishing.

0 Kudos
PrevinWong1
Esri Contributor

esri has a solution called ArcREST.  A python package that handles all the publishing details to ArcGIS Server, Portal, and ArcGIS online.  Give it a look here.  You configure a json file and execute it via python.

Esri/ArcREST · GitHub

NolanAlbarelli
Occasional Contributor

thanks micah!

0 Kudos