<?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: Publish and overwrite web layers in ModelBuilder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298503#M67958</link>
    <description>&lt;P&gt;You also still haven't fixed the error that's going to run in line 14.&lt;/P&gt;</description>
    <pubDate>Tue, 13 Jun 2023 12:41:33 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2023-06-13T12:41:33Z</dc:date>
    <item>
      <title>Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298175#M67937</link>
      <description>&lt;P&gt;I am trying to create a phyton script to create a geoprocessing tool to Publish and overwrite web layers within ModelBuilder. I am referring to the instructions within this blog post:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-pro/mapping/publish-and-overwrite-web-layers-in-modelbuilder/" target="_blank"&gt;https://www.esri.com/arcgis-blog/products/arcgis-pro/mapping/publish-and-overwrite-web-layers-in-modelbuilder/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The issue that I’m having is the script provided in the link above contains the input “map” and what I would like to select is an input “layer” in the content pane.&lt;/P&gt;&lt;P&gt;Jonah Lay has provided sample code within the comments of the blog post here: &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm#GUID-8E27A3ED-A705-4ACF-8C7D-AA861327AD26" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm#GUID-8E27A3ED-A705-4ACF-8C7D-AA861327AD26&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The issue is when I try to use that code I continue to receive errors. Is there someone that could help debug this issue? I have zero coding experience.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 16:46:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298175#M67937</guid>
      <dc:creator>jennahlers</dc:creator>
      <dc:date>2023-06-12T16:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298187#M67938</link>
      <description>&lt;P&gt;It would be helpful if you posted what code you're using, (see &lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank" rel="noopener"&gt;formatting guide here&lt;/A&gt;) as well as your error messages.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 17:07:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298187#M67938</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-12T17:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298200#M67939</link>
      <description>&lt;P&gt;Hi AlfredBaldenweck,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I’m using:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import xml.dom.minidom as DOM

def ScriptTool(map, service, summary, tags, description, overwriteService, portalFolder, timezone, federatedServerUrl, share_public, share_organization, share_groups, outdir):
    """ScriptTool function docstring"""

    # Set output file names
    sddraft_filename = service + ".sddraft"
    sddraft_output_filename = os.path.join(outdir, sddraft_filename)

    # Reference layers to publish
  aprx = arcpy.mp.ArcGISProject("CURRENT")
	m = aprx.listlayers('CURRENT')[0]
    selected_layer = m.listLayers()[0]

    # Create MapImageSharingDraft and set service properties
    server_type = "FEDERATED_SERVER"
    federated_server_url = federatedServerUrl
    sharing_draft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service)
    sharing_draft.federatedServerUrl = federated_server_url
    sharing_draft.summary = summary
    sharing_draft.tags = tags
    sharing_draft.description = description
    sharing_draft.credits = "My Credits"
    sharing_draft.useLimitations = "My Use Limitations"
    sharing_draft.overwriteExistingService = overwriteService
    sharing_draft.portalFolder = portalFolder

    # Create Service Definition Draft file
    sharing_draft.exportToSDDraft(sddraft_output_filename)
    outsddraft = sddraft_output_filename
    arcpy.AddMessage("Service definition draft created")

    # Set time zone
    if (timezone != ""):
        property_set = [{
            "key": "dateFieldsRespectsDayLightSavingTime",
            "value": "true"
        },
            {
                "key": "dateFieldsTimezoneID",
                "value": timezone
            }]
        SetTimezone(sddraft_output_filename, property_set=property_set)

    # Create Service Definition file
    sd_filename = service + ".sd"
    sd_output_filename = os.path.join(outdir, sd_filename)
    arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
    arcpy.AddMessage("Service definition created")

    # Share to portal
    output = arcpy.UploadServiceDefinition_server(sd_output_filename, federated_server_url,
                                                  in_override="OVERRIDE_DEFINITION", in_public=share_public,
                                                  in_organization=share_organization, in_groups=share_groups)
    arcpy.AddMessage("Service published")

    return output[5]

def SetTimezone(sddraftPath, property_set):
    # Read the sddraft xml.
    doc = DOM.parse(sddraftPath)

    # Find all elements named TypeName. This is where the server object extension
    # (SOE) names are defined.
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        # Get the TypeName to enable
        if typeName.firstChild.data == "MapServer":
            extension = typeName.parentNode
            # prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
            for extElement in extension.childNodes:
                if extElement.tagName == 'Definition':
                    for definition in extElement.childNodes:
                        if definition.tagName == 'ConfigurationProperties':
                            for config_prop in definition.childNodes:
                                if config_prop.tagName == 'PropertyArray':
                                    for prop in property_set:
                                        prop_set = doc.createElement("PropertySetProperty")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "typens:PropertySetProperty"
                                        prop_set.setAttributeNode(attr)

                                        prop_key = doc.createElement("Key")
                                        txt = doc.createTextNode(prop["key"])
                                        prop_key.appendChild(txt)
                                        prop_set.appendChild(prop_key)

                                        prop_value = doc.createElement("Value")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "xs:string"
                                        prop_value.setAttributeNode(attr)
                                        txt = doc.createTextNode(prop["value"])
                                        prop_value.appendChild(txt)
                                        prop_set.appendChild(prop_value)

                                        config_prop.appendChild(prop_set)

    # Write to the .sddraft file
    f = open(sddraftPath, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("Time zone set")
    return

if __name__ == '__main__':
    # ScriptTool parameters
    map = arcpy.GetParameter(0)
    service = arcpy.GetParameterAsText(1)
    summary = arcpy.GetParameterAsText(2)
    tags = arcpy.GetParameterAsText(3)
    description = arcpy.GetParameterAsText(4)
    overwriteService = arcpy.GetParameter(5)
    portalFolder = arcpy.GetParameterAsText(6)
    timezone = arcpy.GetParameterAsText(7)
    federatedServerUrl = arcpy.GetParameterAsText(8)
    share_public = arcpy.GetParameterAsText(9)
    share_organization = arcpy.GetParameterAsText(10)
    share_groups = arcpy.GetParameterAsText(11)
    outdir = arcpy.GetParameterAsText(12)

    rest_endpoint = ScriptTool(map, service, summary, tags, description, overwriteService, portalFolder, timezone, federatedServerUrl, share_public, share_organization, share_groups, outdir)
    arcpy.SetParameterAsText(1, rest_endpoint)
    arcpy.AddMessage(rest_endpoint)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and this is the error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jennahlers_0-1686590577010.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/72934iE17842E531CE1306/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jennahlers_0-1686590577010.png" alt="jennahlers_0-1686590577010.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 17:23:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298200#M67939</guid>
      <dc:creator>jennahlers</dc:creator>
      <dc:date>2023-06-12T17:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298207#M67940</link>
      <description>&lt;P&gt;That's an easy fix so far.&lt;/P&gt;&lt;P&gt;Basically that line (#13) is out of line with what's around it. Make it line up with lines 12 and 14 and that particular error will go away.&lt;/P&gt;&lt;P&gt;Python recognizes things like loops or parts of functions by the indentation.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def funct():
    # Everything spaced here belongs to funct
#This is a different thing
def funct3():
    # This belongs to funct3
    exList = [1,2,3]
    for e in exList:
        # Everything in here belongs to this loop
    # Then when that's done, I can continue on with what I was doing&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Depending on what you're writing in (example Notepad++), there should be a setting to show you indentation lines/ white spaces.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1686591209466.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/72937i22A433599CDB7D2F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1686591209466.png" alt="AlfredBaldenweck_0-1686591209466.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 17:33:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298207#M67940</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-12T17:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298208#M67941</link>
      <description>&lt;P&gt;Oops, I published that before I should have.&lt;/P&gt;&lt;P&gt;Make sure when creating that draft that you specify the layer(s) you want.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sharing_draft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service, [selected_layer])&lt;/LI-CODE&gt;&lt;P&gt;You should also make sure that that's actually the layer you want to use; right now you're just grabbing the top layer. If you specify by name, you should be able to grab exactly the right one.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selected_layer = m.listLayers("Grazing Allotments 2023")[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, you're specifying a Map Image.&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm#:~:text=service_type%2C%20service_name%2C%20%7Blayers_and_tables%7D)-,Parameter,-Explanation" target="_blank" rel="noopener"&gt; Is that the type of service you want?&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 17:42:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298208#M67941</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-12T17:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298363#M67946</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;&amp;nbsp;, you've identified I wasn't using the correct sample code. (I'm trying to overwrite a weblayer) I've attached the new code and fixed the code that wasn't in line. I've also attached my new error but I'm feeling the solution is around specifying the layers I want. I'm just not sure what line I need to add that code into?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import xml.dom.minidom as DOM

def ScriptTool(map, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir):
    """ScriptTool function docstring"""

    # Set output file names
    sddraft_filename = service + ".sddraft"
    sddraft_output_filename = os.path.join(outdir, sddraft_filename)

    # Reference layers to publish
    aprx = arcpy.mp.ArcGISProject("CURRENT")
    m = aprx.listlayers('CURRENT')[0]
    selected_layer = m.listLayers()[0]

    # Create FeatureSharingDraft and set service properties
    sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service)
    sharing_draft.summary = summary
    sharing_draft.tags = tags
    sharing_draft.description = description
    sharing_draft.credits = "My Credits"
    sharing_draft.useLimitations = "My Use Limitations"
    sharing_draft.overwriteExistingService = overwriteService

    # Create Service Definition Draft file
    sharing_draft.exportToSDDraft(sddraft_output_filename)
    outsddraft = sddraft_output_filename
    arcpy.AddMessage("Service definition draft created")

    # Modify capabilities
    if enableEditing or enableSync:
        ModifyCapabilities(sddraft_output_filename, enableEditing, enableSync)
    if enableWFS:
        EnableWFS(sddraft_output_filename)
    
    # Set time zone
    if(timezone != ""):
        property_set = [{
            "key": "dateFieldsRespectsDayLightSavingTime",
            "value": "true"
        },
            {
                "key": "dateFieldsTimezoneID",
                "value": timezone
            }]
        SetTimezone(sddraft_output_filename, property_set=property_set)

    # Create Service Definition file
    sd_filename = service + ".sd"
    sd_output_filename = os.path.join(outdir, sd_filename)
    arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
    arcpy.AddMessage("Service definition created")

    # Upload to portal
    output = arcpy.UploadServiceDefinition_server(sd_output_filename, "My Hosted Services", in_override="OVERRIDE_DEFINITION", in_public=share_public, in_organization=share_organization, in_groups=share_groups)
    arcpy.AddMessage("Service published")

    return output[5]

def ModifyCapabilities(sddraft_output_filename, enableEditing, enableSync):
    capabilities = "Query"
    if enableEditing:
        capabilities += ",Create,Delete,Update,Editing"
    if enableSync:
        capabilities += ",Sync"
    # Modify feature layer capabilities to enable Create and Sync
    doc = DOM.parse(sddraft_output_filename)
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        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 = capabilities
                                                else:
                                                    txt = doc.createTextNode(capabilities)
                                                    prop1.nextSibling.appendChild(txt)
    # Write to the .sddraft file
    f = open(sddraft_output_filename, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("Capabilities updated")
    return

def EnableWFS(sddraft_output_filename):
    doc = DOM.parse(sddraft_output_filename)
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        # Get the TypeName to enable
        if typeName.firstChild.data == "EnableWFSServer":
            extension = typeName.parentNode
            for extElement in extension.childNodes:
                # Enable feature access
                if extElement.tagName == 'Enabled':
                    extElement.firstChild.data = 'true'
    # Write to the .sddraft file
    f = open(sddraft_output_filename, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("WFS set")
    return

def SetTimezone(sddraftPath, property_set):
    # Read the sddraft xml
    doc = DOM.parse(sddraftPath)

    # Find all elements named TypeName. This is where the server object extension
    # (SOE) names are defined.
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        # Get the TypeName to enable
        if typeName.firstChild.data == "MapServer":
            extension = typeName.parentNode
            # prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
            for extElement in extension.childNodes:
                if extElement.tagName == 'Definition':
                    for definition in extElement.childNodes:
                        if definition.tagName == 'ConfigurationProperties':
                            for config_prop in definition.childNodes:
                                if config_prop.tagName == 'PropertyArray':
                                    for prop in property_set:
                                        prop_set = doc.createElement("PropertySetProperty")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "typens:PropertySetProperty"
                                        prop_set.setAttributeNode(attr)

                                        prop_key = doc.createElement("Key")
                                        txt = doc.createTextNode(prop["key"])
                                        prop_key.appendChild(txt)
                                        prop_set.appendChild(prop_key)

                                        prop_value = doc.createElement("Value")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "xs:string"
                                        prop_value.setAttributeNode(attr)
                                        txt = doc.createTextNode(prop["value"])
                                        prop_value.appendChild(txt)
                                        prop_set.appendChild(prop_value)

                                        config_prop.appendChild(prop_set)

    # Write to the .sddraft file
    f = open(sddraftPath, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("Timezone set")
    return

if __name__ == '__main__':
    # ScriptTool parameters
    map = arcpy.GetParameter(0)
    service = arcpy.GetParameterAsText(1)
    summary = arcpy.GetParameterAsText(2)
    tags = arcpy.GetParameterAsText(3)
    description = arcpy.GetParameterAsText(4)
    overwriteService = arcpy.GetParameter(5)
    enableEditing = arcpy.GetParameter(6)
    enableSync = arcpy.GetParameter(7)
    enableWFS = arcpy.GetParameter(8)
    timezone = arcpy.GetParameterAsText(9)
    share_public = arcpy.GetParameterAsText(10)
    share_organization = arcpy.GetParameterAsText(11)
    share_groups = arcpy.GetParameterAsText(12)
    outdir = arcpy.GetParameterAsText(13)
    
    rest_endpoint = ScriptTool(map, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir)
    arcpy.SetParameterAsText(1, rest_endpoint)
    arcpy.AddMessage(rest_endpoint)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error Message:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Publish Web Feature Layer&lt;BR /&gt;=====================&lt;BR /&gt;Parameters&lt;/P&gt;&lt;P&gt;Layer MODIS 15km Buffer Current Day&lt;BR /&gt;Web Layer Name Draft&lt;BR /&gt;Summary&lt;BR /&gt;Tags&lt;BR /&gt;Description&lt;BR /&gt;Overwrite Existing Web Layer true&lt;BR /&gt;Enable Editing&lt;BR /&gt;Enable Sync&lt;BR /&gt;Enable WFS&lt;BR /&gt;Time zone&lt;BR /&gt;Share With Everyone PRIVATE&lt;BR /&gt;Share With Organization SHARE_ORGANIZATION&lt;BR /&gt;Share With Groups&lt;BR /&gt;Output Directory S:\GIS\Projects\GIS0000\GIS0014_PECL_Wildfire_Webmap\GIS0014_MODIS_HotSpot_Buffer\output&lt;BR /&gt;Rest Endpoint&lt;BR /&gt;=====================&lt;BR /&gt;Messages&lt;/P&gt;&lt;P&gt;Start Time: Monday, June 12, 2023 4:09:11 PM&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "S:\GIS\Projects\GIS0000\GIS0014_PECL_Wildfire_Webmap\Python\overwriteweblay.py", line 175, in &amp;lt;module&amp;gt;&lt;BR /&gt;rest_endpoint = ScriptTool(map, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir)&lt;BR /&gt;File "S:\GIS\Projects\GIS0000\GIS0014_PECL_Wildfire_Webmap\Python\overwriteweblay.py", line 14, in ScriptTool&lt;BR /&gt;m = aprx.listlayers('CURRENT')[0]&lt;BR /&gt;AttributeError: 'ArcGISProject' object has no attribute 'listlayers'&lt;/P&gt;&lt;P&gt;Failed script Publish Web Feature Layer...&lt;BR /&gt;Failed to execute (PublishWebFeatureLayer).&lt;BR /&gt;Failed at Monday, June 12, 2023 4:09:13 PM (Elapsed Time: 2.00 seconds)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 22:17:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298363#M67946</guid>
      <dc:creator>jennahlers</dc:creator>
      <dc:date>2023-06-12T22:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298365#M67947</link>
      <description>&lt;P&gt;Oops, I didn't catch that before.&lt;/P&gt;&lt;P&gt;Line 14 should be&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;m = aprx.listMaps("Map")[0]

# OR

m = aprx.activeMap&lt;/LI-CODE&gt;&lt;P&gt;(change to suit your needs).&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 22:23:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298365#M67947</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-12T22:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298377#M67948</link>
      <description>&lt;P&gt;I'm slowly getting there &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/458875"&gt;@AlfredBaldenweck&lt;/a&gt;&amp;nbsp;I don't want it to overwrite an entire map just a single feature layer. This is my updated code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import xml.dom.minidom as DOM

def ScriptTool(  layer, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir):
    """ScriptTool function docstring"""

    # Set output file names
    sddraft_filename = service + ".sddraft"
    sddraft_output_filename = os.path.join(outdir, sddraft_filename)

    # Reference layers to publish
    aprx = arcpy.mp.ArcGISProject("CURRENT")
    m = aprx.listLayers("layer")[0] 
    selected_layer = m.listLayer()[0]

    # Create FeatureSharingDraft and set service properties
    sharing_draft = m.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service)
    sharing_draft.summary = summary
    sharing_draft.tags = tags
    sharing_draft.description = description
    sharing_draft.credits = "My Credits"
    sharing_draft.useLimitations = "My Use Limitations"
    sharing_draft.overwriteExistingService = overwriteService

    # Create Service Definition Draft file
    sharing_draft.exportToSDDraft(sddraft_output_filename)
    outsddraft = sddraft_output_filename
    arcpy.AddMessage("Service definition draft created")

    # Modify capabilities
    if enableEditing or enableSync:
        ModifyCapabilities(sddraft_output_filename, enableEditing, enableSync)
    if enableWFS:
        EnableWFS(sddraft_output_filename)
    
    # Set time zone
    if(timezone != ""):
        property_set = [{
            "key": "dateFieldsRespectsDayLightSavingTime",
            "value": "true"
        },
            {
                "key": "dateFieldsTimezoneID",
                "value": timezone
            }]
        SetTimezone(sddraft_output_filename, property_set=property_set)

    # Create Service Definition file
    sd_filename = service + ".sd"
    sd_output_filename = os.path.join(outdir, sd_filename)
    arcpy.StageService_server(sddraft_output_filename, sd_output_filename)
    arcpy.AddMessage("Service definition created")

    # Upload to portal
    output = arcpy.UploadServiceDefinition_server(sd_output_filename, "My Hosted Services", in_override="OVERRIDE_DEFINITION", in_public=share_public, in_organization=share_organization, in_groups=share_groups)
    arcpy.AddMessage("Service published")

    return output[5]

def ModifyCapabilities(sddraft_output_filename, enableEditing, enableSync):
    capabilities = "Query"
    if enableEditing:
        capabilities += ",Create,Delete,Update,Editing"
    if enableSync:
        capabilities += ",Sync"
    # Modify feature layer capabilities to enable Create and Sync
    doc = DOM.parse(sddraft_output_filename)
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        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 = capabilities
                                                else:
                                                    txt = doc.createTextNode(capabilities)
                                                    prop1.nextSibling.appendChild(txt)
    # Write to the .sddraft file
    f = open(sddraft_output_filename, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("Capabilities updated")
    return

def EnableWFS(sddraft_output_filename):
    doc = DOM.parse(sddraft_output_filename)
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        # Get the TypeName to enable
        if typeName.firstChild.data == "EnableWFSServer":
            extension = typeName.parentNode
            for extElement in extension.childNodes:
                # Enable feature access
                if extElement.tagName == 'Enabled':
                    extElement.firstChild.data = 'true'
    # Write to the .sddraft file
    f = open(sddraft_output_filename, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("WFS set")
    return

def SetTimezone(sddraftPath, property_set):
    # Read the sddraft xml
    doc = DOM.parse(sddraftPath)

    # Find all elements named TypeName. This is where the server object extension
    # (SOE) names are defined.
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        # Get the TypeName to enable
        if typeName.firstChild.data == "MapServer":
            extension = typeName.parentNode
            # prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
            for extElement in extension.childNodes:
                if extElement.tagName == 'Definition':
                    for definition in extElement.childNodes:
                        if definition.tagName == 'ConfigurationProperties':
                            for config_prop in definition.childNodes:
                                if config_prop.tagName == 'PropertyArray':
                                    for prop in property_set:
                                        prop_set = doc.createElement("PropertySetProperty")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "typens:PropertySetProperty"
                                        prop_set.setAttributeNode(attr)

                                        prop_key = doc.createElement("Key")
                                        txt = doc.createTextNode(prop["key"])
                                        prop_key.appendChild(txt)
                                        prop_set.appendChild(prop_key)

                                        prop_value = doc.createElement("Value")
                                        attr = doc.createAttribute("xsi:type")
                                        attr.value = "xs:string"
                                        prop_value.setAttributeNode(attr)
                                        txt = doc.createTextNode(prop["value"])
                                        prop_value.appendChild(txt)
                                        prop_set.appendChild(prop_value)

                                        config_prop.appendChild(prop_set)

    # Write to the .sddraft file
    f = open(sddraftPath, 'w')
    doc.writexml(f)
    f.close()
    arcpy.AddMessage("Timezone set")
    return

if __name__ == '__main__':
    # ScriptTool parameters
    map = arcpy.GetParameter(0)
    service = arcpy.GetParameterAsText(1)
    summary = arcpy.GetParameterAsText(2)
    tags = arcpy.GetParameterAsText(3)
    description = arcpy.GetParameterAsText(4)
    overwriteService = arcpy.GetParameter(5)
    enableEditing = arcpy.GetParameter(6)
    enableSync = arcpy.GetParameter(7)
    enableWFS = arcpy.GetParameter(8)
    timezone = arcpy.GetParameterAsText(9)
    share_public = arcpy.GetParameterAsText(10)
    share_organization = arcpy.GetParameterAsText(11)
    share_groups = arcpy.GetParameterAsText(12)
    outdir = arcpy.GetParameterAsText(13)
    
    rest_endpoint = ScriptTool(layer, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir)
    arcpy.SetParameterAsText(1, rest_endpoint)
    arcpy.AddMessage(rest_endpoint)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and this is my new error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Publish Web Feature Layer&lt;BR /&gt;=====================&lt;BR /&gt;Parameters&lt;/P&gt;&lt;P&gt;Layer MODIS 15km Buffer Current Day&lt;BR /&gt;Web Layer Name HotSpot Test&lt;BR /&gt;Summary&lt;BR /&gt;Tags&lt;BR /&gt;Description&lt;BR /&gt;Overwrite Existing Web Layer true&lt;BR /&gt;Enable Editing&lt;BR /&gt;Enable Sync&lt;BR /&gt;Enable WFS&lt;BR /&gt;Time zone&lt;BR /&gt;Share With Everyone PRIVATE&lt;BR /&gt;Share With Organization SHARE_ORGANIZATION&lt;BR /&gt;Share With Groups&lt;BR /&gt;Output Directory S:\GIS\Projects\GIS0000\GIS0014_PECL_Wildfire_Webmap\GIS0014_MODIS_HotSpot_Buffer\output&lt;BR /&gt;Rest Endpoint&lt;BR /&gt;=====================&lt;BR /&gt;Messages&lt;/P&gt;&lt;P&gt;Start Time: Monday, June 12, 2023 4:45:51 PM&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "S:\GIS\Projects\GIS0000\GIS0014_PECL_Wildfire_Webmap\Python\overwriteweblay.py", line 175, in &amp;lt;module&amp;gt;&lt;BR /&gt;rest_endpoint = ScriptTool( layer, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir)&lt;BR /&gt;NameError: name 'layer' is not defined&lt;/P&gt;&lt;P&gt;Failed script Publish Web Feature Layer...&lt;BR /&gt;Failed to execute (PublishWebFeatureLayer).&lt;BR /&gt;Failed at Monday, June 12, 2023 4:45:53 PM (Elapsed Time: 1.75 seconds)&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 22:46:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298377#M67948</guid>
      <dc:creator>jennahlers</dc:creator>
      <dc:date>2023-06-12T22:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298378#M67949</link>
      <description>&lt;P&gt;&lt;SPAN&gt;NameError: name 'layer' is not defined&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You don't define what 'layer' is in your inputs to the tool&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# ---  Where is 'layer' ???? ----
if __name__ == '__main__':
    # ScriptTool parameters
    map = arcpy.GetParameter(0)
    service = arcpy.GetParameterAsText(1)
    summary = arcpy.GetParameterAsText(2)
    tags = arcpy.GetParameterAsText(3)
    description = arcpy.GetParameterAsText(4)
    overwriteService = arcpy.GetParameter(5)
    enableEditing = arcpy.GetParameter(6)
    enableSync = arcpy.GetParameter(7)
    enableWFS = arcpy.GetParameter(8)
    timezone = arcpy.GetParameterAsText(9)
    share_public = arcpy.GetParameterAsText(10)
    share_organization = arcpy.GetParameterAsText(11)
    share_groups = arcpy.GetParameterAsText(12)
    outdir = arcpy.GetParameterAsText(13)
    
    rest_endpoint = ScriptTool(layer, service, summary, tags, description, overwriteService, enableEditing, enableSync, enableWFS, timezone, share_public, share_organization, share_groups, outdir)
    arcpy.SetParameterAsText(1, rest_endpoint)
    arcpy.AddMessage(rest_endpoint)&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 12 Jun 2023 22:53:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298378#M67949</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-06-12T22:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298379#M67950</link>
      <description>&lt;P&gt;It's confused because you changed "map" in line 175 to "layer", but you haven't told it what "layer" is. Change it back to "map"&lt;/P&gt;&lt;P&gt;To make sure you're only uploading the layer, not the map, you list the layer(s) in line 18&lt;/P&gt;&lt;PRE&gt;sharing_draft = m.getWebLayerSharingDraft(server_type, "MAP_IMAGE", service, [selected_layer])&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 22:55:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298379#M67950</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-12T22:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: Publish and overwrite web layers in ModelBuilder</title>
      <link>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298503#M67958</link>
      <description>&lt;P&gt;You also still haven't fixed the error that's going to run in line 14.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jun 2023 12:41:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-and-overwrite-web-layers-in-modelbuilder/m-p/1298503#M67958</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-06-13T12:41:33Z</dc:date>
    </item>
  </channel>
</rss>

