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 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':
desc.firstChild.data = '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
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")
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.
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
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'
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