AnsweredAssumed Answered

How to modify cache properties in service definition draft before upload to server?

Question asked by on Apr 14, 2017
Latest reply on Sep 4, 2017 by

Hi all,

I was wondering how modify service defition draft for cache level before uploading to server.

Following sample 6 of help page (CreateMapSDDraft—Help | ArcGIS Desktop) I was able to insert some further steps from the original code:

  • turn on cache
  • set "create tile on demand"
  • set minimum and maximum scale for cache

Here is my code

import arcpy
from arcpy import env
import xml.dom.minidom as DOM
import os, sys, time, datetime, traceback, string

pathFile = arcpy.GetParameterAsText(0)
server = arcpy.GetParameterAsText(1)
serviceFolder = arcpy.GetParameterAsText(2)
serviceName = arcpy.GetParameterAsText(3)
summary = arcpy.GetParameterAsText(4)
tags = arcpy.GetParameterAsText(5)

# define local variables
arcpy.env.overwriteOutput = True
wrkspc = arcpy.env.scratchFolder
connectionFolder = r"C:\Users\<User>\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog"
mapServer = os.path.join(connectionFolder, server, serviceName + '.MapServer')
connectionFile =  os.path.join(connectionFolder, server +'.ags')

# create service definition draft
copy_data_to_server = True
sddraft =  os.path.join(wrkspc, serviceName + '.sddraft')
sd = os.path.join(wrkspc, serviceName + '.sd')
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER', connectionFile, copy_data_to_server, serviceFolder, summary, tags)

# The Server Object Extension (SOE) to disable.
soe = 'KmlServer'

# Read the sddraft xml.
doc = DOM.parse(sddraft)
# Find all elements named TypeName. This is where the server object extension (SOE) names are defined.
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
    # Get the TypeName we want to disable.
    if == soe:
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Disabled SOE.
            if extElement.tagName == 'Enabled':
       = 'false'

# turn on caching in the configuration properties
configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
propArray = configProps.firstChild
propSets = propArray.childNodes
for propSet in propSets:
    keyValues = propSet.childNodes
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if == "isCached":
                # turn on caching
       = "true"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if == "cacheOnDemand":
                # turn on caching on demand
       = "true"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if == "minScale":
                # set min scale
       = "591657527,591555"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if == "maxScale":
                # set max scale
       = "1128,497176"

# Output to a new sddraft.
outXml = os.path.join(wrkspc, serviceName + 'Update.sddraft')
f = open(outXml, 'w')
doc.writexml( f )

sddraft = outXml

# 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, connectionFile)
    # if the sddraft analysis contained errors, display them
    print analysis['errors']


But this is the result



Anyone can help me?