MapServiceDraft

Summary

The MapServiceDraft class allows you to configure map service properties and create a service definition draft (.sddraft) file, which can be shared to ArcGIS Server.

Discussion

To create a MapServiceDraft object, use the arcpy.sharing.CreateSharingDraft function and set the server_type parameter to STANDALONE_SERVER and the service_type parameter to MAP_SERVICE. The MapServiceDraft object can then be configured by setting service-level properties. Use the targetServer property to specify the server to which the map service is published. After the MapServiceDraft object is configured, it can be saved to a service definition draft (.sddraft) file using the exportToSDDraft function. It can then be staged and shared to ArcGIS Server using the Stage Service and Upload Service Definition tools. For more information, see Introduction to arcpy.sharing.

Once the .sddraft file has been staged and uploaded to the server, the tools in the Caching toolset can be used to create and manage map server caches for faster display of map services, such as the Manage Map Server Cache Tiles tool. The tiling scheme can also be modified by editing the .sddraft file using XML libraries such as the xml.dom.minidom library. However, due to the complexity of the tiling scheme XML structure, it is recommended that you use the Caching toolset whenever possible.

Properties

PropertyExplanationData Type
copyDataToServer
(Read and Write)

Specifies whether the data in your map will be copied to the server. A value of True will copy all of the data in your map, including data that is registered with your server. A value of False will only copy data that is not registered with your server; data registered with your server will be referenced by the service.

Boolean
credits
(Read and Write)

The credits of the map service draft.

String
description
(Read and Write)

The description of the map service draft.

String
offline
(Read and Write)

Specifies whether to use a server connection. If set to False, you must provide a server URL or an ArcGIS Server connection file (.ags) to the targetServer property to create a service definition draft (.sddraft) file using the exportToSDDraft function. If set to True, a service definition draft file can be created without populating the targetServer property.

Boolean
overwriteExistingService
(Read and Write)

Specifies whether an existing service with the same name will be overwritten.

Boolean
serverFolder
(Read and Write)

The name of a server folder to which you want to publish the service. The default folder is the root folder of the server. The folder will be created if it does not already exist.

String
serverType
(Read Only)

The server type as specified when the MapServiceDraft was created from the CreateSharingDraft function. The only possible value returned from serverType for a MapServiceDraft is STANDALONE_SERVER. A serverType of STANDALONE_SERVER indicates support for creating a map service for ArcGIS Server.

String
serviceName
(Read and Write)

The name of the map service. This is the name people will see and use to identify the service. The name can only contain alphanumeric characters and underscores. No spaces or special characters are allowed. The name cannot be more than 120 characters in length.

String
summary
(Read and Write)

The summary of the map service draft.

String
tags
(Read and Write)

The tags of the map service draft. Tags can be separated by commas or semicolons.

String
targetServer
(Read and Write)

The server to which the map will be published. The server can be specified using either of the following formats:

  • The full path to an ArcGIS Server connection file (.ags).
  • An ArcGIS Server URL. Use this option when you have a publisher or administrator connection to ArcGIS Server in the ArcGIS Pro project and you are opening the project in the script or running the script in ArcGIS Pro.

For more information, see Connect to a GIS server.

Tip:

The targetServer can also be used in the in_server parameter in the Upload Service Definition tool. The code samples below demonstrate this.

String
useLimitations
(Read and Write)

The use limitations of the map service draft.

String

Method Overview

MethodExplanation
exportToSDDraft (out_sddraft)

Converts a MapServiceDraft to a service definition draft (.sddraft) file.

Methods

exportToSDDraft (out_sddraft)
ParameterExplanationData Type
out_sddraft

The path and file name for the output service definition draft (.sddraft) file.

String

Once the MapServiceDraft is configured, it can be saved as a service definition draft (.sddraft) file. It can then be staged and shared to ArcGIS Server using the Stage Service and Upload Service Definition tools.

Code sample

MapServiceDraft example

The following script publishes a map in an ArcGIS Pro project as a map service to ArcGIS Server.

import arcpy
import os

# Set output file names
outdir = r"C:\Project\Output"
service = "MapServiceDraftExample"
sddraft_filename = service + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)

# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps("World")[0]

# Create MapServiceDraft and set service properties
service_draft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service, m)
service_draft.targetServer = r"C:\Project\gisserver.ags.esri.com (publisher).ags"

# Create Service Definition Draft file
service_draft.exportToSDDraft(sddraft_output_filename)

# Stage Service
sd_filename = service + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
arcpy.StageService_server(sddraft_output_filename, sd_output_filename)

# Publish to server
print("Uploading Service Definition...")
arcpy.UploadServiceDefinition_server(sd_output_filename, "https://gisserver.esri.com:6443/arcgis/")

print("Successfully Uploaded service.")
Modify SDDraft example

The following script creates a map service definition draft (.sddraft) file for a map. It then enables feature access on the map service by modifying the service definition draft file using the xml.dom.minidom standard Python library. The modified service definition file is then published to ArcGIS Server. Finally, the script calls the Manage Map Server Cache Tiles tool to create map service cache tiles.

import arcpy, os, sys
import xml.dom.minidom as DOM

arcpy.env.overwriteOutput = True

# Update these variables
serviceName = "ModifySDDraftExample"
tempPath = r"C:\Project\Output"
path2APRX = r"C:\Project\World.aprx"
# Make sure this server url is added as publisher ags connection to the project
# Else use the ags connection file itself
targetServer = "https://gisserver.esri.com:6443/arcgis/"

# All paths are built by joining names to the tempPath
SDdraftPath = os.path.join(tempPath, "tempdraft.sddraft")
newSDdraftPath = os.path.join(tempPath, "updatedDraft.sddraft")
SDPath = os.path.join(tempPath, serviceName + ".sd")

aprx = arcpy.mp.ArcGISProject(path2APRX)
m = aprx.listMaps('World')[0]

# Create MapServiceDraft and set service properties
sddraft = arcpy.sharing.CreateSharingDraft('STANDALONE_SERVER', 'MAP_SERVICE', serviceName, m)
sddraft.targetServer = targetServer
sddraft.copyDataToServer = False
sddraft.exportToSDDraft(SDdraftPath)

# Read the contents of the original SDDraft into an xml parser
doc = DOM.parse(SDdraftPath)

# The following code modifies the SDDraft from a new MapService with caching capabilities
# to a FeatureService with Map, Create and Query capabilities.
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
    if typeName.firstChild.data == "FeatureServer":
        extention = typeName.parentNode
        for extElement in extention.childNodes:
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = 'true'

# Turn off caching
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":
                keyValue.nextSibling.firstChild.data = "false"

# Turn on feature access capabilities
configProps = doc.getElementsByTagName('Info')[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 == "WebCapabilities":
                keyValue.nextSibling.firstChild.data = "Map,Query,Data"
                
# Modify keep cache, false by default
configProps = doc.getElementsByTagName('KeepExistingMapCache')[0]
configProps.firstChild.data = "true"
                
# Write the new draft to disk
f = open(newSDdraftPath, 'w')
doc.writexml(f)
f.close()

try:
    # Stage the service
    arcpy.StageService_server(newSDdraftPath, SDPath)
    warnings = arcpy.GetMessages(1)
    print(warnings)
    print("Staged service")

    # Upload the service
    arcpy.UploadServiceDefinition_server(SDPath, server_con)
    print("Uploaded service")

    # Manage Map server Cache Tiles
    # For cache, use multiple scales seperated by semicolon (;) 
    # For example "591657527.591555;295828763.795777" 
    arcpy.server.ManageMapServerCacheTiles(targetServer + os.sep + serviceName + ".MapServer", "591657527.591555",
    "RECREATE_ALL_TILES")
    
except Exception as stage_exception:
    print("Analyzer errors encountered - {}".format(str(stage_exception)))

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))