MapImageSharingDraft

サマリー

The MapImageSharingDraft class allows you to create a service definition draft file (.sddraft) for a map image layer that copies all data or references registered data to ArcGIS Enterprise.

説明

To create the MapImageSharingDraft object, use the Map object's getWebLayerSharingDraft method and set the service_type parameter to MAP_IMAGE. The MapImageSharingDraft object can then be configured by setting service level properties and metadata properties. Use the federatedServerUrl property to specify the federated server to which the web layer is published.

注意:

メタデータのプロパティ (creditsdescriptionsummarytagsuseLimitations) が設定されていないか空の文字列で構成されている場合、Web レイヤー アイテムは、共有されるものに応じてマップまたはレイヤーからメタデータを供給します。 Web レイヤーがメタデータを供給する方法については、「Web レイヤーのメタデータ」をご参照ください。

After the MapImageSharingDraft object has been configured, it can be saved to a service definition draft file (.sddraft) using the exportToSDDraft method. It can then be staged and shared to ArcGIS Enterprise using the Stage Service and Upload Service Definition tools. For more information, see Introduction to arcpy.sharing.

Code samples are available for the following:

プロパティ

プロパティ説明データ タイプ
checkUniqueIDAssignment
(読み書き)

A Boolean that indicates whether your map is analyzed to confirm that the Allow assignment of unique numeric IDs for sharing web layers option in Map Properties is enabled. For more information, see Assign layer IDs.

Boolean
copyDataToServer
(読み書き)

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

Boolean
credits
(読み書き)

A string that represents the credits.

String
description
(読み書き)

A string that represents the description.

String
federatedServerUrl
(読み書き)

A string representing the URL to the ArcGIS Enterprise portal federated server.

ヒント:

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

String
offline
(読み書き)

A Boolean that determines whether to use a portal connection. If offline is set to False, you must be signed in to a portal as well as provide an ArcGIS Enterprise portal federated server URL to the federatedServerUrl property to create a service definition draft file (.sddraft) using the exportToSDDraft function. If offline is set to True, a service definition draft file can be created without being signed in to a portal and without populating the federatedServerUrl property.

Boolean
offlineTarget
(読み書き)

A string that determines the target server version to which the service definition will be published. Specifying a version ensures the service definition contains content compatible with your server. This property is only honored if offline is set to True.

  • ENTERPRISE_10x—Content will be compatible with ArcGIS Enterprise 10.9.1 or earlier. This is the default.
  • ENTERPRISE_11—Content will be compatible with ArcGIS Enterprise 11.0 or later.
String
overwriteExistingService
(読み書き)

A Boolean that determines whether to overwrite an existing web layer.

Boolean
portalFolder
(読み書き)

A string that represents the name of an existing portal folder to which you want to publish the web layer. The default folder is your root folder in My Content.

String
serverFolder
(読み書き)

A string that represents the name of a server folder to which you want to publish the service. The default folder is the root folder of the federated server.

String
serverType
(読み取り専用)

Returns a string representing the server type as specified when the sharing draft was created from the getWebLayerSharingDraft function from the map class. The only possible value returned from serverType for a MapImageSharingDraft is FEDERATED_SERVER. A serverType of FEDERATED_SERVER indicates support for sharing to an ArcGIS Enterprise portal federated server.

String
serviceName
(読み書き)

A string that represents the name of the web layer. 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.

String
summary
(読み書き)

A string that represents the summary.

String
tags
(読み書き)

A string that represents the tags. Multiple tags can be added or separated by a comma or semicolon.

String
useLimitations
(読み書き)

A string that represents the use limitations.

String

方法の概要

方法説明
exportToSDDraft (out_sddraft)

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

方法

exportToSDDraft (out_sddraft)
パラメーター説明データ タイプ
out_sddraft

A string that represents the path and file name for the output service definition draft (.sddraft) file.

String

Once the MapImageSharingDraft has been configured, it can be saved as a service definition draft (.sddraft) file. It can then be staged and shared to the ArcGIS Enterprise portal federated server using the Stage Service and Upload Service Definition tools.

コードのサンプル

Publish a map image layer to a portal folder

The following script creates a map image layer service definition draft file (.sddraft) for a map and sets metadata properties. The map image layer is published to a folder in ArcGIS Enterprise and shared with everyone (public). Portal information is obtained from the SignInToPortal function.

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

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft and set metadata, portal folder, and server folder properties
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url
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.portalFolder = "my folder name"
sddraft.serverFolder = "MyServerFolder"

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

# Read the .sddraft file
docs = DOM.parse(sddraft_output_filename)
key_list = docs.getElementsByTagName('Key')
value_list = docs.getElementsByTagName('Value')

# Change following to "true" to share
SharetoOrganization = "false"
SharetoEveryone = "true"
SharetoGroup = "false"
# If SharetoGroup is set to "true", uncomment line below and provide group IDs
GroupID = ""    # GroupID = "f07fab920d71339cb7b1291e3059b7a8, e0fb8fff410b1d7bae1992700567f54a"

# Each key has a corresponding value. In all the cases value of key_list[i] is value_list[i]
for i in range(key_list.length):
    if key_list[i].firstChild.nodeValue == "PackageUnderMyOrg":
        value_list[i].firstChild.nodeValue = SharetoOrganization
    if key_list[i].firstChild.nodeValue == "PackageIsPublic":
        value_list[i].firstChild.nodeValue = SharetoEveryone
    if key_list[i].firstChild.nodeValue == "PackageShareGroups":
        value_list[i].firstChild.nodeValue = SharetoGroup
    if SharetoGroup == "true" and key_list[i].firstChild.nodeValue == "PackageGroupIDs":
        value_list[i].firstChild.nodeValue = GroupID

# Write to the .sddraft file
f = open(sddraft_output_filename, 'w')
docs.writexml(f)
f.close()

# 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, federated_server_url)

print("Finish Publishing")
Overwrite a map image layer

The following script overwrites a map image layer. If the service name already exists, the service will be overwritten. Otherwise, a new service will be created.

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft and set overwrite property
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url
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)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)

print("Finish Publishing")
Publish a map image layer with an associated feature layer

The following script creates a map image layer service definition draft (.sddraft) file. It then includes a feature layer and sets feature layer and map image layer properties by modifying the service definition draft file using the xml.dom.minidom standard Python library. The modified service definition draft file is then staged and published to an ArcGIS Enterprise portal federated server. The data store must be already registered on the server for the data to be referenced.

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

def configure_featureserver_capabilities(sddraftPath, capabilities):
    """Function to configure FeatureServer properties"""
    # 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 == "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':
                                        if prop1.nextSibling.hasChildNodes():
                                            prop1.nextSibling.firstChild.data = capabilities
                                        else:
                                            txt = doc.createTextNode(capabilities)
                                            prop1.nextSibling.appendChild(txt)
    # Write to the .sddraft file
    f = open(sddraftPath, 'w')
    doc.writexml(f)
    f.close()

def configure_mapserver_capabilities(sddraftPath, capabilities):
    """Function to configure MapServer properties"""
    # 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 == "MapServer":
            extension = typeName.parentNode
            for extElement in extension.childNodes:
                if extElement.tagName == 'Definition':
                    for propArray in extElement.childNodes:
                        if propArray.tagName == 'Info':
                            for propSet in propArray.childNodes:
                                for prop in propSet.childNodes:
                                    for prop1 in prop.childNodes:
                                        if prop1.tagName == "Key":
                                            if prop1.firstChild.data == 'WebCapabilities':
                                                if prop1.nextSibling.hasChildNodes():
                                                    prop1.nextSibling.firstChild.data = capabilities
                                                else:
                                                    txt = doc.createTextNode(capabilities)
                                                    prop1.nextSibling.appendChild(txt)
    # Write to the .sddraft file
    f = open(sddraftPath, 'w')
    doc.writexml(f)
    f.close()

if __name__ == "__main__":
    # Sign in to portal
    arcpy.SignInToPortal("https://portal.domain.com/webadaptor", "MyUserName", "MyPassword")

    # Set output file names
    outdir = r"C:\Project\Output"
    service_name = "MapImageSharingDraftExample"
    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 MapImageSharingDraft and set copyDataToServer property to False to reference registered data
    server_type = "FEDERATED_SERVER"
    federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
    sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
    sddraft.federatedServerUrl = federated_server_url
    sddraft.copyDataToServer = False

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

    """Modify the .sddraft file to include a feature layer and set map image layer and feature layer properties"""

    # Modify the .sddraft file to change map image layer properties
    # Defaults are Map,Query,Data
    # Comment out the line below if you do not want to modify map image layer properties
    configure_mapserver_capabilities(sddraft_output_filename, "Map,Data")

    # Modify the .sddraft file to include a feature layer
    # 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:
                # Include a feature layer
                if extElement.tagName == 'Enabled':
                    extElement.firstChild.data = 'true'

    # Write to 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()

    # Modify the .sddraft file to change feature layer properties
    # Defaults are Query,Create,Update,Delete,Uploads,Editing
    # Comment out the line below if you don't want to modify feature layer properties
    configure_featureserver_capabilities(sddraft_mod_xml_file, "Create,Sync,Query")

    # Stage Service
    print("Start Staging")
    arcpy.server.StageService(sddraft_mod_xml_file, sd_output_filename)

    # Share to portal
    print("Start Uploading")
    arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)

    print("Finish Publishing")
Publish a cached map image layer

The following script publishes a cached map image layer by calling the Manage Map Server Cache Tiles tool to create map service cache tiles.

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

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url

# 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)

    # Share to portal
    print("Start Uploading")
    arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)
    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(federated_server_url + "/" + "rest/services" + "/" + 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))
Publish a map image layer that draws from existing cache

The following script creates a map image layer service definition draft (.sddraft) file. It then uses an existing cache to draw the service by modifying the service definition draft file using the xml.dom.minidom standard Python library. The modified service definition draft file is then staged and published to an ArcGIS Enterprise portal federated server.

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

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url

# 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)

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)

print("Finish Publishing")
Analyze and register a data store

The following script creates a service definition draft (.sddraft) file for a map image layer. The service definition draft file is analyzed during staging. If analyzer 24011 is returned warning that the data source is not registered with the server, the data store is registered using the arcgis.gis module in the ArcGIS API for Python.

import arcpy
import os

# Add a data store item on the portal and register it with a federated server
def register_datastore_item(portal_url, username, password, sde_conn_file, data_store_name, federated_server_url):
    from arcgis.gis import GIS

    tags = "tag1, tag2"
    description = "Data Store Item created from ArcGIS API for Python"
    federated_server_url = federated_server_url.rstrip('/')
    federated_server_adminurl = federated_server_url + "/admin"

    # Connect to your portal using ArcGIS API for Python
    gis = GIS(url=portal_url, username=username, password=password)

    # Get federated server ID
    server_list = gis.servers["servers"]
    fedserver_dict = [srvr for srvr in server_list if srvr["url"] == federated_server_url][0]
    fedserver_id = fedserver_dict["id"]
    print("Server id: " + fedserver_id)

    # Get federated server's DatastoreManager object
    fedserver = [srvr for srvr in gis.admin.servers.list() if srvr.url == federated_server_adminurl][0]
    fedserver_dsmgr = fedserver.datastores

    # Create a connection string from .sde file connection
    conn_file = sde_conn_file
    conn_string = fedserver_dsmgr.generate_connection_string(conn_file)
    print("Connection string generated")

    # Create a dictionary for data store configuration
    ds_config = {"type": "egdb",
                 "path": "/enterpriseDatabases/" + data_store_name,
                 "info": {"isManaged": "false",
                          "dataStoreConnectionType": "shared",
                          "connectionString": conn_string}
                 }

    # Create a dictionary for the item properties
    item_properties = {"title": data_store_name,
                       "type": "Data Store",
                       "tags": tags,
                       "snippet": description}

    # Add the data store item to the portal
    ds_item = gis.content.add(item_properties=item_properties,
                              text=ds_config)
    print("Data store item added")

    # Get portal data store
    portal_ds = gis.datastore
    
    # Validate the data store item can be connected to before registering
    validation_state = portal_ds.validate(server_id=fedserver_id,
                                          item=ds_item)
    print("Data store item validated")

    # Register the database with the federated server
    portal_ds.register(item=ds_item,
                       server_id=fedserver_id,
                       bind=False)
    print("Data store item registered to server")
    return True


if __name__ == "__main__":
    arcpy.env.overwriteOutput = True
    portal_url = "https://portal.domain.com/webadaptor"
    username = "MyUserName"
    password = "MyPassword"

    # Sign in to portal
    arcpy.SignInToPortal(portal_url, username, password)

    # Set output file names
    outdir = r"C:\Project\Output"
    service_name = "registerdatastore_example"
    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)
    sde_conn_file = r"C:\Project\db_conn.sde"
    data_store_name = "PortalDSItem"

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

    # Create MapImageSharingDraft and set copyDataToServer property to False to reference registered data
    server_type = "FEDERATED_SERVER"
    federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
    sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
    sddraft.federatedServerUrl = federated_server_url
    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
            register_res = register_datastore_item(portal_url, username, password, sde_conn_file, data_store_name,
                                                   federated_server_url)
            if not register_res:
                print("Unable to register the data store, skipping upload")
                exit()
            # Stage the service again
            stage_service = True
        else:
            stage_service = False

    # Share to portal
    print("Start Uploading")
    arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)

    print("Finish Publishing")
Set a time zone and configure pooling

The following script creates a map image layer service definition draft (.sddraft) file. It then sets a time zone for layers with date fields and configures service instance settings by modifying the service definition draft file using the xml.etree.ElementTree and xml.dom.minidom standard Python libraries. The modified service definition draft file is then staged and published to an ArcGIS Enterprise portal federated server. The data store must be already registered on the server for the data to be referenced.

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:
                # Include a feature layer
                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__":
    # Sign in to portal
    arcpy.SignInToPortal("https://portal.domain.com/webadaptor", 
"MyUserName", "MyPassword")

    # Set output file names
    outdir = r"C:\Project\Output"
    service_name = "MapImageSharingDraftExample"
    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 MapImageSharingDraft and set copyDataToServer property to False to reference registered data
    server_type = "FEDERATED_SERVER"
    federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
    sddraft = m.getWebLayerSharingDraft(server_type, 'MAP_IMAGE', service_name)
    sddraft.federatedServerUrl = federated_server_url
    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)

    # Share to portal
    print("Start Uploading")
    arcpy.server.UploadServiceDefinition(sd_output_filename, federated_server_url)

    print("Finish Publishing")
Publish a layer and table

The following script publishes a layer and table as a map image layer to an ArcGIS Enterprise portal.

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 layers to publish
aprx = arcpy.mp.ArcGISProject(r"C:\Project\World.aprx")
m = aprx.listMaps('World')[0]
selected_layer = m.listLayers()[0]
selected_table = m.listTables()[0]

# Create MapImageSharingDraft
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name, [selected_layer, selected_table])
sddraft.federatedServerUrl = federated_server_url

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

# Stage Service
print("Start Staging")
arcpy.server.StageService(sddraft_output_filename, sd_output_filename)

# Share to portal
print("Start Uploading")
arcpy.UploadServiceDefinition_server(sd_output_filename, federated_server_url)

print("Finish Publishing")
Create and publish an offline service definition

The following script creates an offline service definition for the target server version and publishes it as a map image layer.

import arcpy
import os

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft and set offline and offlineTarget properties
server_type = "FEDERATED_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.offline = True
# Set offlineTarget property to "ENTERPRISE_11" for ArcGIS Enterprise 11.0 or later
# The default is ArcGIS Enterprise 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)

# Sign in to portal to upload and publish
arcpy.SignInToPortal("https://portal.domain.com/webadaptor", 
"MyUserName", "MyPassword")

# Share to portal
print("Start Uploading")
arcpy.server.UploadServiceDefinition(sd_output_filename, "https://MyFederatedServer.domain.com/serverWebadaptor")

print("Finish Publishing")
Analyze to confirm map is set to allow assignment of unique numeric IDs

The following script creates a map image layer service definition draft file (.sddraft) that enables the checkUniqueIDAssignment property. The service definition draft file is analyzed during staging. If the map is not set to allow assignment of unique numeric IDs for sharing web layers, analyzer error 00374 is returned and a message is printed. For more information, see Assign layer IDs.

import arcpy
import os

# Sign in to portal
arcpy.SignInToPortal("https://portal.domain.com/webadaptor",
                     "MyUserName", "MyPassword")

# Set output file names
outdir = r"C:\Project\Output"
service_name = "MapImageSharingDraftExample"
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 MapImageSharingDraft and set the check unique ID assignment property
server_type = "FEDERATED_SERVER"
federated_server_url = "https://MyFederatedServer.domain.com/serverWebadaptor"
sddraft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service_name)
sddraft.federatedServerUrl = federated_server_url
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))

関連トピック