<?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 Using python to publish a map service in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46686#M3741</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can someone help me with a python script to publish a feature layer as a map service (overwrite map/feature service).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 04 Jun 2015 13:54:48 GMT</pubDate>
    <dc:creator>NolanAlbarelli</dc:creator>
    <dc:date>2015-06-04T13:54:48Z</dc:date>
    <item>
      <title>Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46686#M3741</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can someone help me with a python script to publish a feature layer as a map service (overwrite map/feature service).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2015 13:54:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46686#M3741</guid>
      <dc:creator>NolanAlbarelli</dc:creator>
      <dc:date>2015-06-04T13:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46687#M3742</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Nolan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Take a look at this post:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://gis.stackexchange.com/questions/61057/overwrite-existing-map-service-in-arcgis-server-10-1-through-arcpy/61140#61140" title="http://gis.stackexchange.com/questions/61057/overwrite-existing-map-service-in-arcgis-server-10-1-through-arcpy/61140#61140"&gt;Overwrite existing map service in ArcGIS Server 10.1 (through ArcPy) - Geographic Information Systems Stack Exchange&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2015 14:18:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46687#M3742</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2015-06-04T14:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46688#M3743</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Greetings Nolan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple questions for you:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Does the service you'd like to overwrite have "Feature Access" enabled? In other words, does it need to be editable?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. Is the service you'd like to overwrite published on your own on-premise ArcGIS Server, or on Esri's "My Hosted Services?"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For an on-premise map service that does not require feature access, the following script (set up for a script tool but could be modified to be run stand-alone outside of ArcGIS), should get you there:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# overwrite_service.py
# Created: 3/11/2015
# Author:&amp;nbsp;&amp;nbsp; Micah Babinski
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; City of Portland
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bureau of Technology Services
&lt;SPAN&gt;#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;A class="jive-link-email-small" href="mailto:micah.babinski@portlandoregon.gov" rel="nofollow noopener noreferrer" target="_blank"&gt;micah.babinski@portlandoregon.gov&lt;/A&gt;
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 503.823.1091
#
# Description: Overwrites an existing map service on ArcGIS Server
#


import arcpy
import xml.dom.minidom as DOM


# User parameters
map_document = arcpy.GetParameterAsText(0)
service_folder = arcpy.GetParameterAsText(1)
service_name = arcpy.GetParameterAsText(2)


# Create the MXD object and define the summary and tags
arcpy.AddMessage("Creating an MXD object and retrieving the summary and tags.")
mxd = arcpy.mapping.MapDocument(map_document)
summary = mxd.summary
tags = mxd.tags


# Define the workspace path
arcpy.AddMessage("Defining the workspace path.")
workspace = arcpy.Describe(map_document).path + "\\"


# Define the AGS connection
arcpy.AddMessage("Defining the AGS connection file.")
con = #r"Path to your arcgis server connection eg: GIS Servers\arcgis on myserver_6080 (admin)"


# Create a variable representing the output service definition draft
sddraft = workspace + service_name + '.sddraft'


# Create a variable representing the output service draft
sd = workspace + service_name + '.sd'


# Create service definition draft
arcpy.AddMessage("Creating the service definition draft.")
analysis = arcpy.mapping.CreateMapSDDraft(mxd, sddraft, service_name, 'ARCGIS_SERVER', con, False, service_folder, summary, tags)


# Set service type to esriServiceDefinitionType_Replacement
arcpy.AddMessage("Setting the service type to be a replacement.")
newType = 'esriServiceDefinitionType_Replacement'
xml = sddraft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if desc.parentNode.tagName == 'SVCManifest':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if desc.hasChildNodes():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; desc.firstChild.data = newType
outXml = xml&amp;nbsp;&amp;nbsp;&amp;nbsp; 
f = open(outXml, 'w')&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
doc.writexml( f )&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
f.close()


# Stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute StageService
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Staging the service.")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.StageService_server(sddraft, sd)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute UploadServiceDefinition
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Uploading the service.")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.UploadServiceDefinition_server(sd, con)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Upload successful!")
else: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # If the sddraft analysis contained errors, display them
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("The following errors prevented overwriting of the service:")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(analysis['errors'])


# Remove the sd draft and sd files
arcpy.AddMessage("Cleaning up.")
arcpy.Delete_management(sd)
arcpy.Delete_management(sddraft)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now if you need feature access, it gets more complicated &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;. I haven't figured out how to overwrite an on-premise editable feature service with Python yet, but I have &lt;A _jive_internal="true" href="https://community.esri.com/thread/139716" target="_blank"&gt;asked about it on geonet&lt;/A&gt;. For overwriting an Esri-hosted feature service, there is a code sample &lt;A href="https://github.com/khibma/update-hosted-feature-service/blob/master/update.py" rel="nofollow noopener noreferrer" target="_blank"&gt;here&lt;/A&gt;, but I haven't tested it yet.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you figure out how to overwrite the on-premise editable service, PLEASE let me know. Happy scripting!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Micah&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:49:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46688#M3743</guid>
      <dc:creator>MicahBabinski</dc:creator>
      <dc:date>2021-12-10T21:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46689#M3744</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;esri has a solution called ArcREST.&amp;nbsp; A python package that handles all the publishing details to ArcGIS Server, Portal, and ArcGIS online.&amp;nbsp; Give it a look here.&amp;nbsp; You configure a json file and execute it via python.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Esri/ArcREST" title="https://github.com/Esri/ArcREST"&gt;Esri/ArcREST · GitHub&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2015 14:32:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46689#M3744</guid>
      <dc:creator>PrevinWong1</dc:creator>
      <dc:date>2015-06-04T14:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46690#M3745</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have the same issue and the code snipped I'm using to enable feature access is the same as Micah and comes directly from the ESRI help files.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Oct 2015 20:53:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46690#M3745</guid>
      <dc:creator>ToddHenry1</dc:creator>
      <dc:date>2015-10-19T20:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46691#M3746</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Micah,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for sharing that script. I did change it to be used as a stand-alone script, it works like a charm! The one shown in the SA post does not seem to be very robust and your's is very elegant!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 21:17:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46691#M3746</guid>
      <dc:creator>PhilippNagel</dc:creator>
      <dc:date>2016-06-15T21:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46692#M3747</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Cheers, Philipp! Happy publishing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Jun 2016 19:20:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46692#M3747</guid>
      <dc:creator>MicahBabinski</dc:creator>
      <dc:date>2016-06-17T19:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using python to publish a map service</title>
      <link>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46693#M3748</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thanks micah!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jun 2016 15:24:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-to-publish-a-map-service/m-p/46693#M3748</guid>
      <dc:creator>NolanAlbarelli</dc:creator>
      <dc:date>2016-06-21T15:24:24Z</dc:date>
    </item>
  </channel>
</rss>

