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

2004
3
07-14-2016 12:44 PM
DevinUnderwood2
Occasional Contributor

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)\mapservice.sd'


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

  desc.firstChild.data = 'esriServiceDefinitionType_Replacement'
outXml = xml

f = open(outXml, 'w')

doc.writexml(f)

f.close()

# 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")

0 Kudos
3 Replies
ChrisPedrezuela
Occasional Contributor III

Have you tried testing\running it Devin? I've tried this same sample script with updating AGOL services, so I would assume it would work. If there is an error then kindly update this thread.

0 Kudos
DevinUnderwood2
Occasional Contributor

Thank you for taking the time to look at this.

I get the following error.

Traceback (most recent call last):

  File "C:/Users/dunderwood/Documents/AutomatePublishWebLayers/Overwrite_Map_Service.py", line 24, in <module>

    doc = DOM.parse(xml)

  File "C:\Python27\ArcGIS10.2\lib\xml\dom\minidom.py", line 1921, in parse

    return expatbuilder.parse(file)

  File "C:\Python27\ArcGIS10.2\lib\xml\dom\expatbuilder.py", line 922, in parse

    fp = open(file, 'rb')

IOError: [Errno 2] No such file or directory: 'C:\\Users\\dunderwood\\AppData\\Local\\ESRI\\Desktop10.2\\Staging\\ARCGIS on xyz (publisher)\mapservice.sddraft'

0 Kudos
EricTran
New Contributor III

Hi Devin, sorry for being a year and half late, but I see two issues with the script.  The first, and cause of your error, is the position of the xml modification bit.  That needs to go in between steps 1 and 2,  the sddraft has not been created yet and the xml functions can't find the file to modify.

The second bit that may cause you issue are the lines 

  • "   if desc.hasChildNodes():"

and

  •   desc.firstChild.data = 'esriServiceDefinitionType_Replacement'

0 Kudos