Select to view content in your preferred language

Publishing to Portal from GDB/SDE via python

190
1
3 weeks ago
DavidBates-Jefferys
New Contributor

Is there any straight forward way to publish a Feature Service to an Enterprise Portal from a GDB or queried feature class in an SDE using either the Python API or arcpy.

I previously used the Python API's Geoaccesor to pull in polygon feature classes from a GDB, but the Geoaccessor seems limited to pandas dataframes with Pro 3.2

My current work around is to create an sd off a map in an APRX using arcpy & then add the sd to content using the Python API. Though this takes a while to run (10-30 min) and loses any definition queries applied to the layer.

Would love a simple publish from .gdb or federated .sde tool/tutorial.

0 Kudos
1 Reply
BrandonMcAlister
Occasional Contributor

would it be possible to export the queried features to the default Geodatbase then just publish the new layer if so then please see the following:

https://pro.arcgis.com/en/pro-app/3.2/arcpy/mapping/map-class.htm specifically getWebLayerSharingDraft

https://pro.arcgis.com/en/pro-app/3.2/arcpy/sharing/featuresharingdraft-class.htm Follow code samples

sample code:

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

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

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

print("Finish Publishing")

if you run into an error like this: ERROR 001272: Analyzer errors were encountered ([{"code":"0102", "message":"Selected Layer does not contian a required layer type for web feature layer", "object":"Map"}')

then add this before you create the .sddraft:

aprx = arcpy.mp.ArcGISProject("Current")
### BUG FIX ###
#refresh map view and save project
aprx.closeViews("MAPS")
aprx.listMaps("Map")[0].openView()
aprx.save()

 

Thanks,
Brandon
0 Kudos