What is the 'overwrite service' workflow when using service definitions?

06-07-2018 10:41 AM
Occasional Contributor III

Hi All, 

I have a server environment where we publish services via the 'create service definition - no server connection" in ArcMap, hand the .SD file off to an admin that then publishes the service via the web manager.  

There's the option to 'overwrite a service' from ArcMap but it seems like this is only avialable for the connected workflow.  Is this process just a wizard that does the following steps in automation:

1. Create service definition

2. Import from existing service

3. Delete existing

4. Publish new

So our workflow to "overwrite" in this disconnected environment is those 4 manual steps or is there a shortcut? 



0 Kudos
4 Replies
by Anonymous User
Not applicable

I just tried (upload .sd --> reupload .sd with the same name) this through the Manager GUI in a federated 10.5.1 Enterprise deployment and got the error message saying you can only create new services this way.

However, I know this can be accomplished with python. I've broken up the script into two parts that can be run on your two machines, respectively:

1. Machine that creates .sd

##-------------------------Script Info----------------
# Objective: Create a service definition that can be used to overwrite an existing Map Service
# Andrew Valenski
# 6/12/2018
##------------------Defining Variables----------------

# Import necessary modules
import arcpy
import xml.dom.minidom as DOM

# This defines the workspace that the script looks for the .MXD in
workspace = 'C:/TestSDCreation/'

# This is the name of .MXD you're using to create the .sd
mxdName = 'sample.mxd'

# Create a .sd file on your server machine and make a local copy on this machine
connectionFile = 'GIS Servers/arcgis on localhost_6080 (admin)'

# This is the name of the service you're overwriting. 
serviceName = 'MyMapService'

# Service Summary (Included to prevent warnings ... and its good to have)
summary = 'Population Density by County'

# Service Tags (Included to prevent warnings ... and its good to have)
tags = 'county, counties, population, density, census'

##----------------Defining Paths--------------------

finalMXD = arcpy.mapping.MapDocument(workspace+ mxdName)
sdDraft = workspace + serviceName + '.sddraft'
sd = workspace + serviceName + '.sd'

##--------------Creating SD Draft-------------------

# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(finalMXD, sdDraft, serviceName, 'ARCGIS_SERVER', connectionFile, True, None, summary, tags)

##-------------Edit SD Draft------------------------

# set service type to esriServiceDefinitionType_Replacement so it is understood as an 'overwriting' .sd
owType = '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 = owType
outXml = xml    
f = open(outXml, 'w')     
doc.writexml( f )     

##---------Create SD---------------------------

# Check for errors in the previous analysis and, if no errors exists, create the SD
if analysis['errors'] == {}:
    # Execute StageService
    arcpy.StageService_server(sddraft, sd)
    print("Service Successfully Staged at: " + str(sd))
    # if the analysis had errors, stop the publishing and alert the user
    print("SD Draft contains errors: ")
    print("No SD file created")

# Now an SD file is generated that should be copied onto Machine 2 (the GIS Server)

2. Machine 2. uses Python to publish the .sd 

# Objective: Publish SD to GIS Server
# Andrew Valenski
# 6/12/2018

#-----------Import Modules---------------------
import arcpy

#-----------Define Variables-------------------

# This should be the location that you copied the service definition to. The path should
# match your exact path.
finalSD = 'C:\SomeFolderOnServer\someServiceName.sd'

# Create a local .ags connection file and keep it in the folder (keeps things tidy)
connectionFile = 'C:\SomeFolderOnServer\GISServerConnection.ags'

#----------Upload SD to Server-----------------    

# Upload Service Definition (i.e. overwrite existing service using .sd)
arcpy.UploadServiceDefinition_server(sd, con)
print("Service successfully overwritten")

I'd test it out in your environment first, but that should do the trick

0 Kudos
Occasional Contributor III

Thanks Andrew, 

Let me give that second script a try.  I didn't think about trying the arcpy method. If that single method handles overwrite it  might work for us but the admins don't usually have 'desktop' bits installed hence the use of the web manager.  Does the new python api handle this situation? 

I might drop a "allow overwrite in web manager" in the ideas forum. 

0 Kudos
by Anonymous User
Not applicable

The Python API can handle publishing to portal (i.e. AGOL or a Portal for ArcGIS). If you AGE deployment is a Web GIS deployment (i.e. Federated, hosting, etc. etc.) then the API would be able to accomplish this (in simpler terms, I might add). 

0 Kudos
New Contributor III

I am trying to do this as well. To run this, do I need to have the feature class on the map before updating? I keep getting an error message" Dataset does not exist. 



0 Kudos