I am using ArcGIS Desktop and Server 10.1. I want to over write existing map service though ArcPy and going through this link and getting an errorRuntime error Traceback (most recent call last): File "<string>", line 22, in <module> NameError: name 'draftPath' is not defined and I am searching for this at my end.

I just want to know is there any alternative to overwrite map service automatically after 1 hour in ArcGIS Server 10.1 ?

  • Going through following article but its for ArcGIS Online not for the ArcGIS Server (blogs.esri.com/esri/arcgis/2013/04/23/…)
    – Sunil
    Commented May 17, 2013 at 10:55
  • 1
    I guess this sample has something wrong... you need to set sddraft folder path to draftpath variable.
    – Darksanta
    Commented May 17, 2013 at 11:05
  • @Darksanta thanks for reply so do you have the sample code?
    – Sunil
    Commented May 17, 2013 at 11:36
  • I added in: #Delete old .sd arcpy.Delete_management("YourName.sd") print "YourName.sd deleted"
    – user49550
    Commented Mar 25, 2015 at 18:04

2 Answers 2


Here is the sample code which customized from esri sample. This sample follows 3 steps,

  1. Create SDDraft from mxd document
  2. Set service type to esriServiceDefinitionType_Replacement of SDDraft
  3. Stage and upload the sddraft with overwriting existed service.

import arcpy
import xml.dom.minidom as DOM

#define local variables
# wrkspc        mxd document directory
# mxdName       mxd document name
# con           ArcGIS Server Catalog path
# service       service name (include service direcotry)
# summary       service summary
# tags          services tags

wrkspc = 'C:/test/'
mxdName = 'sample.mxd'
con = 'GIS Servers/arcgis on localhost_6080 (admin)' 
service = 'MyMapService'
summary = 'Population Density by County'
tags = 'county, counties, population, density, census'

mapDoc = arcpy.mapping.MapDocument(wrkspc + mxdName)
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'

# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', 
                                          con, True, None, summary, tags)

# set service type to esriServiceDefinitionType_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 )     

# stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
    # Execute StageService
    arcpy.StageService_server(sddraft, sd)
    # Execute UploadServiceDefinition
    arcpy.UploadServiceDefinition_server(sd, con)
    # if the sddraft analysis contained errors, display them
    print analysis['errors']

But make sure that if you are going to overwrite the service within directory such as "Maps/MyMapService", then you need to create "Maps" directory to mxd directory before run script.

  • thanks for this! Sorry for a Python newbie question: but while this code is working well for me if I overwrite a map service in the root directory, I am having trouble overwriting a map service in a sub folder. The comments in the code say to 'include service directory' in the service variable, but I am getting errors when I try this...in your comment you mention 'MXD directory' but OI am not sure how that relates to the service directory? Any help would be appreciated, thanks!
    – JasonBK
    Commented Feb 24, 2015 at 16:18
  • What is SVCManifest? Is that the name of the summary?
    – brad
    Commented Apr 26, 2017 at 15:41

@JasonBK @jasonbk

The CreateMapSDDraft method used in this sample asks for a {folder_name} parameter that was setted to None, so by default it points to root level (see below). If you want to use a sub folder you must specify this instead of None, so in the sample you will use something like this:

analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con, True, 'Maps', summary, tags)

To create (or overwrite) the service in the sub folder named Maps

folder_name{String}: a string that represents a folder name to which you want to publish the service definition. If the folder does not currently exist, it will be created. The default folder is the server root level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.