Synthèse
The FeatureSharingDraft class allows you to create a service definition draft file (.sddraft) for a web feature layer that copies all data to either ArcGIS Enterprise or ArcGIS Online. To configure the properties of a web feature layer that references registered data, modify the MapImageSharingDraft class to include a feature layer.
Discussion
To create the FeatureSharingDraft object, use the Map object's getWebLayerSharingDraft method and set the service_type parameter to FEATURE. The FeatureSharingDraft object can then be configured by setting service level properties and metadata properties.
Remarque :
Si les propriétés des métadonnées (credits, description, summary, tags et useLimitations) ne sont pas définies ou si elles sont composées de chaînes vides, l’élément de la couche Web génère des métadonnées à partir de la carte ou de la couche, selon l’élément partagé. Pour plus d’informations sur la façon dont les couches Web génèrent des métadonnées, consultez la section Métadonnées de la couche Web.
After the FeatureSharingDraft 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 either ArcGIS Enterprise or ArcGIS Online using the Stage Service and Upload Service Definition tools. For more information, see Introduction to arcpy.sharing.
Code samples are available for the following:
Propriétés
Propriété | Explication | Type de données |
allowExporting (Lecture et écriture) | A Boolean that determines whether users can export the web layer to different formats. | Boolean |
checkUniqueIDAssignment (Lecture et écriture) | 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 |
credits (Lecture et écriture) | A string that represents the credits. | String |
description (Lecture et écriture) | A string that represents the description. | String |
offline (Lecture et écriture) | A Boolean that determines whether to use a portal connection. If offline is set to False, you must be signed in to a portal 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. | Boolean |
offlineTarget (Lecture et écriture) | A string that determines the target portal version to which the service definition will be published. Specifying a version ensures the service definition contains content compatible with your portal. This property is only honored if offline is set to True.
| String |
overwriteExistingService (Lecture et écriture) | A Boolean that determines whether to overwrite an existing web layer. | Boolean |
portalFolder (Lecture et écriture) | A string that represents the name of a portal folder to which you want to publish the web layer. The default folder is your root folder in My Content. | String |
serverType (Lecture seule) | 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 FeatureSharingDraft is HOSTING_SERVER. A serverType of HOSTING_SERVER indicates support for sharing to ArcGIS Enterprise or ArcGIS Online. | String |
serviceName (Lecture et écriture) | A string that represents the name of the web layer. This is the name people will see and use to identify the web layer. 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 (Lecture et écriture) | A string that represents the summary. | String |
tags (Lecture et écriture) | A string that represents the tags. Multiple tags can be added or separated by a comma or semicolon. | String |
useLimitations (Lecture et écriture) | A string that represents the use limitations. | String |
Vue d’ensemble des méthodes
Méthode | Explication |
exportToSDDraft (out_sddraft) | Converts a FeatureSharingDraft to a service definition draft (.sddraft) file. |
Méthodes
exportToSDDraft (out_sddraft)
Paramètre | Explication | Type de données |
out_sddraft | A string that represents the path and file name for the output service definition draft (.sddraft) file. | String |
Once the FeatureSharingDraft has been configured, it can then be saved as a service definition draft (.sddraft) file. It can then be staged and shared to either ArcGIS Enterprise or ArcGIS Online using the Stage Service and Upload Service Definition tools.
Exemple de code
The following script creates a web feature layer service definition draft file (.sddraft) for a map and sets metadata and export data properties. The web feature layer is published to a folder in either ArcGIS Online or ArcGIS Enterprise. Portal information is obtained from the SignInToPortal function.
import arcpy
import os
# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft and set metadata, portal folder, and export data properties
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
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.allowExporting = 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, server_type)
print("Finish Publishing")
The following script overwrites a web feature 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://www.arcgis.com", "MyUserName", "MyPassword")
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft and set overwrite property
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
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, server_type)
print("Finish Publishing")
The following script creates a web feature layer service definition draft file (.sddraft). It then sets the create and sync capabilities on the web feature layer 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 a portal.
import arcpy
import os
import xml.dom.minidom as DOM
# Sign in to portal
arcpy.SignInToPortal("https://www.portal.domain.com/webadaptor",
"MyUserName", "MyPassword")
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)
"""Modify the .sddraft file to enable create and sync"""
# Read the .sddraft file
doc = DOM.parse(sddraft_output_filename)
# 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 == '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 = "Create,Sync,Query"
else:
txt = doc.createTextNode("Create,Sync,Query")
prop1.nextSibling.appendChild(txt)
# 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, server_type)
print("Finish Publishing")
The following script publishes a layer and table in a map as a web feature layer to a portal.
import arcpy
import os
# Sign in to portal
arcpy.SignInToPortal("https://www.arcgis.com", "MyUserName", "MyPassword")
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name, [selected_layer, selected_table])
# 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, server_type)
print("Finish Publishing")
The following script creates an offline service definition for the target portal version and publishes it as a web feature layer.
import arcpy
import os
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft and set offline and offlineTarget properties
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
sddraft.offline = True
# Set offlineTarget property to "ONLINE" for ArcGIS Online or "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)
# Sign in to portal to upload and publish
arcpy.SignInToPortal("https://www.portal.domain.com/webadaptor",
"MyUserName", "MyPassword")
# 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")
The following script creates a web feature 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://www.arcgis.com", "MyUserName", "MyPassword")
# Set output file names
outdir = r"C:\Project\Output"
service_name = "FeatureSharingDraftExample"
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 FeatureSharingDraft and set the check unique ID assignment property
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name)
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))
Rubriques connexes
Vous avez un commentaire à formuler concernant cette rubrique ?