<?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 Re: Overwrite weblayers using FeatureSharingDraft class in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1144241#M63763</link>
    <description>&lt;P&gt;Hi Jared,&lt;/P&gt;&lt;P&gt;I realize this is several months old, but did you find a solution?&amp;nbsp; I'm having the exact same issue(s).&amp;nbsp; At first, it didn't overwrite the feature service, it created a new one.&amp;nbsp; Then, I started getting the dreaded 999999 ERROR.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 15 Feb 2022 19:26:55 GMT</pubDate>
    <dc:creator>Levon_H</dc:creator>
    <dc:date>2022-02-15T19:26:55Z</dc:date>
    <item>
      <title>Overwrite weblayers using FeatureSharingDraft class</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1120137#M63060</link>
      <description>&lt;P data-unlink="true"&gt;I've been experimenting with the &lt;EM&gt;FeatureSharingDraft &lt;/EM&gt;class&lt;EM&gt;. &lt;/EM&gt;It works fine accessing map objects indiscriminately, and one at a time. However, I'm trying to access specific objects (layers) in an &lt;EM&gt;if &lt;/EM&gt;statement. Then I want them to &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm#GUID-FDFDD6D8-22BA-4693-A897-C9B10E07E931" target="_self"&gt;&lt;EM&gt;overwrite&lt;/EM&gt;&lt;/A&gt; the corresponding feature services using the&amp;nbsp;&lt;EM&gt;service_names&lt;/EM&gt; list.&lt;/P&gt;&lt;P data-unlink="true"&gt;In my &lt;EM&gt;if &lt;/EM&gt;statement I'm able to find the &lt;EM&gt;selected_layer&lt;/EM&gt; and assign it. However, I'm looking for some help to plug it into the &lt;EM&gt;FeatureSharingDraft &lt;/EM&gt;part of the code. And I also don't think my &lt;EM&gt;service_name&lt;/EM&gt; variable is doing much outside the &lt;EM&gt;maps&lt;/EM&gt; loop, as I'm not sure how to include it.&lt;/P&gt;&lt;P data-unlink="true"&gt;In a nutshell, I want to be able to iterate the &lt;EM&gt;service_names&lt;/EM&gt; list and overwrite the service with the&amp;nbsp;&lt;EM&gt;selected_layer&amp;nbsp;&lt;/EM&gt;from each of the maps in my Pro project.&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#existing service names in my AGOL
service_names = ['Elementary Districts Vacc Rate Tot Pop','Elementary Districts Vacc Rate 12 to 18','Elementary Districts Vacc Rate 0 to 18',
            'Elementary Districts Vacc Rate 11 to 14','Elementary Districts Vacc Rate 14 to 16','Elementary Districts Vacc Rate 16 to 18',
            'Elementary Districts Vacc Rate 19 to 64','Elementary Districts Vacc Rate 65 Plus']

# Set output file names
outdir = r"C:/Users/jpilbeam"
for service_name in service_names:
    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)

'''For each map in the Pro project, 
grab the selected_layer and overwrite the service from the service_name list'''

# Reference maps
maps = aprx.listMaps()

for m in maps:
    for selected_layer in m.listLayers():
        if selected_layer.name.endswith('Elementary'): #&amp;lt;--look for selected layer in map
            print(f'layer: {selected_layer}')

            #---------FeatureSharingDraft------------------
            # Create FeatureSharingDraft and set overwrite property
            server_type = "HOSTING_SERVER"
            # returns a map class object based on service_type selected
            sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name) #&amp;lt;--map class function
            sddraft.overwriteExistingService = True

            # Create and save to service definition draft file
            # returns location and name of sddraft
            sddraft.exportToSDDraft(sddraft_output_filename)

            # Convert to service definition (.sd) file. Staging compiles all necessary info needed to sucessfully publish 
            print("Start Staging")
            arcpy.StageService_server(sddraft_output_filename, sd_output_filename)

            # take .sd file, copy it onto server, extract required info and publish
            print("Start Uploading")
            arcpy.server.UploadServiceDefinition(sd_output_filename, server_type)
            print("Finish Publishing")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried putting &lt;EM&gt;selected_layer &lt;/EM&gt;in the &lt;EM&gt;getWebLayerSharingDraft() &lt;/EM&gt;function as a parameter, but it doesn't ultimately overwrite the feature service. Rather, it publishes a new one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sddraft = prj.getWebLayerSharingDraft(server_type, "FEATURE", service_name, [selected_layer])
sddraft.overwriteExistingService = True&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Nov 2021 21:45:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1120137#M63060</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-11-24T21:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite weblayers using FeatureSharingDraft class</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1121081#M63096</link>
      <description>&lt;P&gt;Hi Jared&lt;/P&gt;&lt;P&gt;I've been working with pretty much the same code today but I'm not doing the outer loop, I am just trying to publish one layer. It creates the SD on my server and then dies with error 999999, no service is created.&lt;/P&gt;&lt;P&gt;I've tried with and without the layer list. Same exact result but without the list, it takes longer since it's copying more data. (I am not sure what it means yet to publish a "FEATURE" service with many layers.)&lt;/P&gt;&lt;P&gt;I am under the impression that line 29 in your script, is putting the service name into the sddraft object and that will determine what the final service name will be.&lt;/P&gt;&lt;PRE&gt;            sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name) #&amp;lt;--map class function&lt;/PRE&gt;&lt;P&gt;I am also hitting the same bug as you where it creates a new service definition in Portal with the same name if I run it more than once. The expectation is that "sddraft.overwriteExistingService = True" should cause it to overwrite.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I posted here&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/uploadservicedefinition-gets-the-good-old-error/m-p/1121071#M63095" target="_blank"&gt;https://community.esri.com/t5/python-questions/uploadservicedefinition-gets-the-good-old-error/m-p/1121071#M63095&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Nov 2021 23:35:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1121081#M63096</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2021-11-29T23:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite weblayers using FeatureSharingDraft class</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1144241#M63763</link>
      <description>&lt;P&gt;Hi Jared,&lt;/P&gt;&lt;P&gt;I realize this is several months old, but did you find a solution?&amp;nbsp; I'm having the exact same issue(s).&amp;nbsp; At first, it didn't overwrite the feature service, it created a new one.&amp;nbsp; Then, I started getting the dreaded 999999 ERROR.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 19:26:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-weblayers-using-featuresharingdraft/m-p/1144241#M63763</guid>
      <dc:creator>Levon_H</dc:creator>
      <dc:date>2022-02-15T19:26:55Z</dc:date>
    </item>
  </channel>
</rss>

