AnsweredAssumed Answered

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

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

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 typeName.firstChild.data == soe:
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Disabled SOE.
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = '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 keyValue.firstChild.data == "isCached":
                # turn on caching
                keyValue.nextSibling.firstChild.data = "true"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if keyValue.firstChild.data == "cacheOnDemand":
                # turn on caching on demand
                keyValue.nextSibling.firstChild.data = "true"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if keyValue.firstChild.data == "minScale":
                # set min scale
                keyValue.nextSibling.firstChild.data = "591657527,591555"
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if keyValue.firstChild.data == "maxScale":
                # set max scale
                keyValue.nextSibling.firstChild.data = "1128,497176"


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

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

 

But this is the result

 

 

Anyone can help me?

 

Stefano

Outcomes