AnsweredAssumed Answered

How to overwrite a published map document as a service via Server

Question asked by EscondidoAnalyst on Jul 14, 2016
Latest reply on Jul 18, 2016 by EscondidoAnalyst

I have the following and needed help on getting this to work. I am unsure about the xml modification of sddraft syntax and order within the script. Any suggestions would be appreciated.


# ArcPy script to overwrite a published map document as a service via Server.
import arcpy,sys

print("Imported ArcPy")

import xml.dom.minidom as DOM



# Set variables
mxd = r'sample.mxd'
server_con = 'ARCGIS_SERVER'
connection_file_path = r"GIS Servers\ARCGIS on xyz (publisher)"
service_name = "themap"
inFolderType = "EXISTING"
inFolder = "Test"
inStartup = "STARTED"
newType = 'esriServiceDefinitionType_Replacement'

# Set output path
sddraft = r'C:\Users\dunderwood\AppData\Local\ESRI\Desktop10.2\Staging\ARCGIS on GIS on xyz (publisher)\mapservice.sddraft'
sd_file = r'C:\Users\dunderwood\AppData\Local\ESRI\Desktop10.2\Staging\ARCGIS on GIS on xyz(publisher)\'

# Method to create a SD file to overwrite an existing service
xml = sddraft

doc = DOM.parse(xml)

descriptions = doc.getElementsByTagName('Type')

for desc in descriptions:

   if desc.parentNode.tagName == 'SVCManifest':

   if desc.hasChildNodes(): = 'esriServiceDefinitionType_Replacement'
outXml = xml

f = open(outXml, 'w')





# 1 Create a service definition draft
analysis_result = arcpy.mapping.CreateMapSDDraft(mxd, sddraft)

print("SDdraft file created")


# 2 Stage and create Service Definition file
arcpy.StageService_server(sddraft, sd_file)

print("Service Definition file created")


# 3 Publish the SD file as a service
arcpy.UploadServiceDefinition_server(sd_file, connection_file_path, service_name,"#",inFolderType,inFolder,inStartup)

print("Service published successfully")