摘要
MapServiceDraft 类允许您为地图服务创建服务定义草稿文件 (.sddraft),该文件将向 ArcGIS Server 复制所有数据或引用注册数据。
说明
要创建 MapServiceDraft 对象,请使用 arcpy.sharing.CreateSharingDraft 函数并将 server_type 参数设置为 STANDALONE_SERVER 以及将 service_type 参数设置为 MAP_SERVICE。 随后即可通过设置服务级别属性和元数据属性来配置 MapServiceDraft 对象。 可以使用 targetServer 属性以指定将地图服务发布到的服务器。
注:
如果未设置元数据属性(credits、description、summary、tags 和 useLimitations)或其具有空字符串,则地图服务将从地图中获取元数据。
有关创作元数据的详细信息,请参阅查看和编辑元数据。
配置 MapServiceDraft 对象后,可以使用 exportToSDDraft 方法将其保存到服务定义草稿文件 (.sddraft)。 然后,可以使用过渡服务和上传服务定义工具将其过渡并共享到 ArcGIS Server。 有关详细信息,请参阅 arcpy.sharing 简介。
将 .sddraft 文件过渡并上传到服务器后,可使用缓存工具集中的工具(如管理地图服务器缓存切片工具)创建和管理地图服务器缓存以加快地图服务显示。 您也可以通过使用 XML 库(如 xml.dom.minidom 库)编辑 .sddraft 文件来修改切片方案。 但是,由于切片方案 XML 结构比较复杂,建议尽量使用缓存工具集。
代码示例可用于以下用途:
属性
属性 | 说明 | 数据类型 |
checkUniqueIDAssignment (可读写) | 一个布尔值,指示是否分析地图以确认启用了地图属性中的允许分配唯一数字 ID 以共享 web 图层选项。 有关详细信息,请参阅分配图层 ID。 | Boolean |
copyDataToServer (可读写) | 指定是否将地图中的数据复制到服务器。 值为 True 时,将复制地图中的所有数据,包括在服务器中注册的数据。 值为 False 时,将仅复制未在服务器中注册的数据;服务将参考已在服务器中注册的数据。 | Boolean |
credits (可读写) | 地图服务草稿的配额。 | String |
description (可读写) | 地图服务草稿的描述。 | String |
offline (可读写) | 指定是否使用服务器连接。 如果设置为 False,则必须向 targetServer 属性提供服务器 URL 或 ArcGIS Server 连接文件 (.ags),才能使用 exportToSDDraft 函数创建服务定义草稿文件 (.sddraft)。 如果设置为 True,则可以在未填充 targetServer 属性的情况下,创建服务定义草稿文件。 | Boolean |
offlineTarget (可读写) | 此字符串用于确定将服务定义发布到的目标服务器版本。 指定一个版本可确保服务定义包含与服务器兼容的内容。 仅在 offline 设置为 True 时支持此属性。
| String |
overwriteExistingService (可读写) | 指定是否覆盖具有相同名称的现有服务。 | Boolean |
serverFolder (可读写) | 要将服务发布到的服务器文件夹名称。 默认文件夹是服务器的根文件夹。 如果不存在此文件夹,则会进行创建。 | String |
serverType (只读) | 通过 CreateSharingDraft 函数创建 MapServiceDraft 时指定的服务器类型。 从 MapServiceDraft 的 serverType 中返回的唯一可能的值为 STANDALONE_SERVER。 STANDALONE_SERVER 的 serverType 表示支持为 ArcGIS Server 创建地图服务。 | String |
serviceName (可读写) | 地图服务的名称。 用户可以看到该名称并使用该名称来识别服务。 名称只能包含字母数字字符和下划线。 不允许使用空格或特殊字符。 名称不能超过 120 个字符。 | String |
summary (可读写) | 地图服务草稿的摘要。 | String |
tags (可读写) | 地图服务草稿的标签。 标签可以逗号或分号分隔。 | String |
targetServer (可读写) | 地图将发布至的服务器。 可以使用以下任一格式指定此服务器:
有关详细信息,请参阅连接到 GIS 服务器。 提示:targetServer 也可以在上传服务定义工具中的 in_server 参数中使用。 以下代码示例对此进行了演示。 | String |
useLimitations (可读写) | 地图服务草稿的使用限制。 | String |
方法概述
方法 | 说明 |
exportToSDDraft (out_sddraft) | 将 MapServiceDraft 转换为服务定义草稿 (.sddraft) 文件。 |
方法
代码示例
以下脚本将为地图服务创建地图服务定义草稿文件 (.sddraft),并设置元数据属性。 地图服务将发布到独立 ArcGIS Server 中的文件夹。
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set metadata and server folder properties
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.credits = "These are credits"
sddraft.description = "This is description"
sddraft.summary = "This is summary"
sddraft.tags = "tag1, tag2"
sddraft.useLimitations = "These are use limitations"
sddraft.serverFolder = "MyServerFolder"
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本将覆盖地图服务。 如果服务名称已存在,则将覆盖该服务。 否则,将创建新服务。
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set overwrite property
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.overwriteExistingService = True
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本将为引用注册数据的地图服务创建服务定义草稿文件 (.sddraft)。 必须已在服务器上注册数据存储,才能引用数据。 然后通过使用 xml.dom.minidom 标准 Python 库,修改服务定义草稿文件,从而允许启用要素功能并设置地图服务和要素服务属性。 然后,会将修改后的服务定义草稿文件过渡并发布到 ArcGIS Server。
import arcpy
import os
import xml.dom.minidom as DOM
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set copyDataToServer property to false to reference registered data
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.copyDataToServer = False
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
"""Modify the .sddraft file to enable the feature capability and set map service and feature service properties"""
# Read the file
doc = DOM.parse(sddraft_output_filename)
# Find all elements named TypeName
# This is where the extensions are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == "FeatureServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
# Enable the feature capability
if extElement.tagName == 'Enabled':
extElement.firstChild.data = 'true'
# Set map service properties
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":
# Defaults are Map,Query,Data
keyValue.nextSibling.firstChild.data = "Map,Data"
# Set feature service properties
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == "FeatureServer":
extension = typeName.parentNode
for extElement in extension.childNodes:
if extElement.tagName == 'Info':
for propSet in extElement.childNodes:
for prop in propSet.childNodes:
for prop1 in prop.childNodes:
if prop1.tagName == "Key":
if prop1.firstChild.data == 'WebCapabilities':
# Defaults are Query,Create,Update,Delete,Uploads,Editing
prop1.nextSibling.firstChild.data = "Create,Sync,Query"
# Write to a new .sddraft file
sddraft_mod_xml = service_name + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file = os.path.join(outdir, sddraft_mod_xml)
f = open(sddraft_mod_xml_file, 'w')
doc.writexml(f)
f.close()
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_mod_xml_file, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本通过调用管理地图服务器缓存切片工具以创建地图服务缓存切片,来发布缓存地图服务。
import arcpy
import os
import xml.dom.minidom as DOM
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
"""Modify the .sddraft to enable caching"""
# Read the file
doc = DOM.parse(sddraft_output_filename)
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 = "true"
# Write to a new .sddraft file
sddraft_mod_xml = service_name + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file = os.path.join(outdir, sddraft_mod_xml)
f = open(sddraft_mod_xml_file, 'w')
doc.writexml(f)
f.close()
try:
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_mod_xml_file, sd_output_filename)
warnings = arcpy.GetMessages(1)
print(warnings)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
# Manage Map server Cache Tiles
# For cache, use multiple scales separated by semicolon (;)
# For example, "591657527.591555;295828763.795777"
arcpy.server.ManageMapServerCacheTiles(target_server_connection + "/" + service_name + ".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))
以下脚本将为地图服务创建服务定义草稿文件 (.sddraft)。 然后该脚本可通过使用 xml.dom.minidom 标准 Python 库,修改服务定义草稿文件,从而使用现有缓存绘制服务。 然后,会将修改后的服务定义草稿文件过渡并发布到 ArcGIS Server。
import arcpy
import os
import xml.dom.minidom as DOM
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
"""Modify the .sddraft file to keep existing cache"""
# Read the file
doc = DOM.parse(sddraft_output_filename)
# 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"
# Use existing cache
configProps = doc.getElementsByTagName('KeepExistingMapCache')[0]
configProps.firstChild.data = "true"
# Write to a new .sddraft file
sddraft_mod_xml = service_name + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file = os.path.join(outdir, sddraft_mod_xml)
f = open(sddraft_mod_xml_file, 'w')
doc.writexml(f)
f.close()
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_mod_xml_file, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本将为地图服务创建服务定义草稿文件 (.sddraft)。 在过渡期间,将对服务定义草稿文件进行分析。 如果返回分析器 24011,警告未将数据源注册到服务器,则将使用 AddDataStoreItem 函数注册数据存储。
import arcpy
import os
arcpy.env.overwriteOutput = True
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set copyDataToServer property to False to reference registered data
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.copyDataToServer = False
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
# Stage the service and analyze the .sddraft file for registered data store
# Continue publishing only if data store is registered
print("Start Staging")
stage_service = True
while stage_service:
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Get analyzer warnings to check if data store is registered
warnings = arcpy.GetMessages(1)
print(warnings)
# If data store is not registered
if "24011" in warnings:
# Register data store
db_conn = r"C:\Project\db_conn.sde"
register_msg = arcpy.AddDataStoreItem(target_server_connection, "DATABASE", "datastore_name", db_conn)
print("Registered datastore: {0}".format(register_msg))
# Stage the service again
stage_service = True
else:
stage_service = False
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本将为地图服务创建服务定义草稿文件 (.sddraft)。 必须已在服务器上注册数据存储,才能引用数据。 它为带有日期字段的图层设置时区,并通过使用 xml.etree.ElementTree 和 xml.dom.minidom 标准 Python 库修改服务定义草稿文件来配置服务实例设置。 然后,会将修改后的服务定义草稿文件过渡并发布到 ArcGIS Server。
import arcpy
import os
import xml.dom.minidom as DOM
import codecs
import xml.etree.ElementTree as ET
def enable_extensions(sddraftPath, soe):
"""Function to enable extensions"""
# Read the .sddraft file
doc = DOM.parse(sddraftPath)
# Find all elements named TypeName
# This is where the additional layers and capabilities are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == soe:
extension = typeName.parentNode
for extElement in extension.childNodes:
# Enable the feature capability
if extElement.tagName == 'Enabled':
extElement.firstChild.data = 'true'
# Write to the .sddraft file
f = open(sddraftPath, 'w')
doc.writexml(f)
f.close()
# soe = extension for which properties must be added
def enable_configproperties(sddraftPath, soe, property_set):
"""Function to configure extension properties"""
# Read the file
doc = DOM.parse(sddraftPath)
# Find all elements named TypeName
# This is where the extensions are defined
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
# Get the TypeName to enable
if typeName.firstChild.data == soe:
extension = typeName.parentNode
# prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
for extElement in extension.childNodes:
if extElement.tagName == 'Definition':
for definition in extElement.childNodes:
if definition.tagName == 'ConfigurationProperties':
for config_prop in definition.childNodes:
if config_prop.tagName == 'PropertyArray':
for prop in property_set:
prop_set = doc.createElement("PropertySetProperty")
attr = doc.createAttribute("xsi:type")
attr.value = "typens:PropertySetProperty"
prop_set.setAttributeNode(attr)
prop_key = doc.createElement("Key")
txt = doc.createTextNode(prop["key"])
prop_key.appendChild(txt)
prop_set.appendChild(prop_key)
prop_value = doc.createElement("Value")
attr = doc.createAttribute("xsi:type")
attr.value = "xs:string"
prop_value.setAttributeNode(attr)
txt = doc.createTextNode(prop["value"])
prop_value.appendChild(txt)
prop_set.appendChild(prop_value)
config_prop.appendChild(prop_set)
# Write to the .sddraft file
f = open(sddraftPath, 'w')
doc.writexml(f)
f.close()
if __name__ == "__main__":
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set copyDataToServer property to False to reference registered data
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.copyDataToServer = False
sddraft.exportToSDDraft(sddraft_output_filename)
# Set time zone to UTC
property_set = [{
"key": "dateFieldsRespectsDayLightSavingTime",
"value": "true"
},
{
"key": "dateFieldsTimezoneID",
"value": "UTC"
}]
enable_configproperties(sddraft_output_filename, soe="MapServer", property_set=property_set)
# Enable extensions on map server
enable_extensions(sddraft_output_filename, "FeatureServer")
# enable_extensions(sddraft_output_filename, "VersionManagementServer")
# enable_extensions(sddraft_output_filename, "LRServer")
# Configure pooling options
doc = DOM.parse(sddraft_output_filename)
def_childnodes = doc.getElementsByTagName("Definition")[0].childNodes
for def_node in def_childnodes:
if def_node.nodeName == "Props":
for node in def_node.childNodes[0].childNodes:
# Change the provider to modify instance type
# provider='DMaps' for shared or 'ArcObjects11' for dedicated
if node.firstChild.firstChild.data == 'provider':
node.lastChild.firstChild.data = 'ArcObjects11'
elif node.firstChild.firstChild.data == 'MinInstances':
node.lastChild.firstChild.data = '0'
elif node.firstChild.firstChild.data == 'MaxInstances':
node.lastChild.firstChild.data = '2'
# Write to the .sddraft file
f = open(sddraft_output_filename, 'w')
doc.writexml(f)
f.close()
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, target_server_connection)
print("Finish Publishing")
以下脚本将为目标服务器版本创建离线服务定义,并将其发布为地图服务。
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set offline and offlineTarget properties
# The targetServer property is not needed when the offline property is set to True
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.offline = True
# Set offlineTarget property to "ENTERPRISE_11" for ArcGIS Server 11.0 or later
# The default is ArcGIS Server 10.9.1 or earlier
sddraft.offlineTarget = "ENTERPRISE_10x"
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Publish to server
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, r"C:\Project\gisserver.ags.esri.com (publisher).ags")
print("Finish Publishing")
以下脚本将为地图服务创建用于启用 checkUniqueIDAssignment 属性的服务定义草案文件 (.sddraft)。 在过渡期间,将对服务定义草稿文件进行分析。 如果地图未设置为允许分配用于共享 web 图层的唯一数字 ID,则会返回分析器错误 00374 并打印一条消息。 有关详细信息,请参阅分配图层 ID。
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapServiceDraftExample"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
# Create MapServiceDraft and set the check unique ID assignment property
target_server_connection = r"C:\Project\gisserver.ags.esri.com (publisher).ags"
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
sddraft.targetServer = target_server_connection
sddraft.checkUniqueIDAssignment = True
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
try:
# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)
print("Finish Publishing")
except Exception as stage_exception:
if "00374" in str(stage_exception):
print("The map is not set to allow assignment of unique IDs")
print("Analyzer errors encountered - {}".format(str(stage_exception)))
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))