<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Publishing to Portal from GDB/SDE via python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/publishing-to-portal-from-gdb-sde-via-python/m-p/1479592#M70668</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I previously used the Python API's Geoaccesor to pull in polygon&amp;nbsp;feature classes from a GDB, but the Geoaccessor seems limited to pandas dataframes with Pro 3.2&lt;/P&gt;&lt;P&gt;My current work around is to create an sd off a map in an APRX using arcpy &amp;amp; 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.&lt;/P&gt;&lt;P&gt;Would love a simple publish from .gdb or federated .sde tool/tutorial.&lt;/P&gt;</description>
    <pubDate>Tue, 28 May 2024 15:38:15 GMT</pubDate>
    <dc:creator>DavidBates-Jefferys</dc:creator>
    <dc:date>2024-05-28T15:38:15Z</dc:date>
    <item>
      <title>Publishing to Portal from GDB/SDE via python</title>
      <link>https://community.esri.com/t5/python-questions/publishing-to-portal-from-gdb-sde-via-python/m-p/1479592#M70668</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I previously used the Python API's Geoaccesor to pull in polygon&amp;nbsp;feature classes from a GDB, but the Geoaccessor seems limited to pandas dataframes with Pro 3.2&lt;/P&gt;&lt;P&gt;My current work around is to create an sd off a map in an APRX using arcpy &amp;amp; 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.&lt;/P&gt;&lt;P&gt;Would love a simple publish from .gdb or federated .sde tool/tutorial.&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2024 15:38:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publishing-to-portal-from-gdb-sde-via-python/m-p/1479592#M70668</guid>
      <dc:creator>DavidBates-Jefferys</dc:creator>
      <dc:date>2024-05-28T15:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: Publishing to Portal from GDB/SDE via python</title>
      <link>https://community.esri.com/t5/python-questions/publishing-to-portal-from-gdb-sde-via-python/m-p/1479617#M70671</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.2/arcpy/mapping/map-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.2/arcpy/mapping/map-class.htm&lt;/A&gt;&amp;nbsp;specifically getWebLayerSharingDraft&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.2/arcpy/sharing/featuresharingdraft-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.2/arcpy/sharing/featuresharingdraft-class.htm&lt;/A&gt;&amp;nbsp;Follow code samples&lt;/P&gt;&lt;P&gt;sample code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;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"}')&lt;/P&gt;&lt;P&gt;then add this before you create the .sddraft:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;aprx = arcpy.mp.ArcGISProject("Current")
### BUG FIX ###
#refresh map view and save project
aprx.closeViews("MAPS")
aprx.listMaps("Map")[0].openView()
aprx.save()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2024 16:14:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publishing-to-portal-from-gdb-sde-via-python/m-p/1479617#M70671</guid>
      <dc:creator>BrandonMcAlister</dc:creator>
      <dc:date>2024-05-28T16:14:29Z</dc:date>
    </item>
  </channel>
</rss>

