<?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 Publish a Map Image Layer with Feature Service Enabled - Feature Layer Not Nested - Using arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/publish-a-map-image-layer-with-feature-service/m-p/1339929#M69266</link>
    <description>&lt;P&gt;Is it possible to publish a single layer programmatically so that it is not nested in it's Feature Service. When I publish a layer using the below script and I add the resulting Feature Service to Pro from Portal, it adds as a nested layer - My Layer\My Layer - but if you publish a Layer using the normal workflow from Pro by right clicking the Layer in the contents pane and selecting Sharing &amp;gt; Share As Web Layer, adding it to the project will only add the inner layer, not the nested layer.&lt;/P&gt;&lt;P&gt;Not sure if that makes sense but can try explain more clearly if required. Following screenshots might help to clarify as well.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MatthewGeorge_0-1697975459039.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83654iF98C0600EBF1EE7D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MatthewGeorge_0-1697975459039.png" alt="MatthewGeorge_0-1697975459039.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm using the following code to do the publishing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    aprx = arcpy.mp.ArcGISProject("CURRENT")
    m = aprx.activeMap

    #check that the map is valid
    if m == None:
        utils.logAndNotify(logger, "No Map is currently active", "ERROR")
        return
    
    #get the layer
    lyr = m.listLayers(lyrName)[0]

    #get and set the values in the SD draft
    utils.logAndNotify(logger, "Setting values to use in the SD draft")
    sdd = m.getWebLayerSharingDraft("FEDERATED_SERVER", "MAP_IMAGE", lyrName, lyr)
    sdd.federatedServerUrl = svrDict[db]['server']
    sdd.description = desc
    sdd.tags = tags
    sdd.serverFolder = svrDict[db]['folder']

    #export to SD draft file
    utils.logAndNotify(logger, "Exporting to file")
    sdd.exportToSDDraft(sddFile)

    #read the output xml file
    utils.logAndNotify(logger, "Updating the SDD XML file")
    sddXml = md.parse(sddFile)
    xmlKeys = sddXml.getElementsByTagName("Key")
    xmlValues = sddXml.getElementsByTagName("Value")

    #set sharing settings
    everyone = "false"
    group = "false"

    #set values required
    for i in range(xmlKeys.length):
        if xmlKeys[i].firstChild.nodeValue == "PackageUnderMyOrg":
            xmlValues[i].firstChild.nodeValue = org
        if xmlKeys[i].firstChild.nodeValue == "PackageIsPublic":
            xmlValues[i].firstChild.nodeValue = everyone
        if xmlKeys[i].firstChild.nodeValue == "PackageShareGroups":
            xmlValues[i].firstChild.nodeValue = group
        if xmlKeys[i].firstChild.nodeValue == "CallingContext":
            xmlValues[i].firstChild.nodeValue = "0"
        if xmlKeys[i].firstChild.nodeValue == "useMapServiceLayerID":
            xmlValues[i].firstChild.nodeValue = "true"

    #get the type name nodes
    typeNameNodes = sddXml.getElementsByTagName("TypeName")
    for typeNameNode in typeNameNodes:
        if typeNameNode.firstChild.data != "MapServer":
            continue

        utils.logAndNotify(logger, "Setting MAP SERVER settings")

        #get the parent node
        parentNode = typeNameNode.parentNode
        
        #loop through the first child
        for node1 in parentNode.childNodes:
            if node1.tagName != "Definition":
                continue

            #loop through the second child
            for node2 in node1.childNodes:
                if node2.tagName not in ["Info", "ConfigurationProperties","Props"]:
                    continue

                #loop through third child - property array
                for propArray in node2.childNodes:
                    
                    #set the timezone details
                    if node2.tagName == "ConfigurationProperties":
                        dtPropSet = sddXml.createElement("PropertySetProperty")
                        attr = sddXml.createAttribute("xsi:type")
                        attr.value = "typens:PropertySetProperty"
                        dtPropSet.setAttributeNode(attr)
                        dtKey = sddXml.createElement("Key")
                        txt = sddXml.createTextNode("dateFieldsTimezoneID")
                        dtKey.appendChild(txt)
                        dtPropSet.appendChild(dtKey)
                        dtValue = sddXml.createElement("Value")
                        attr = sddXml.createAttribute("xsi:type")
                        attr.value = "xs:string"
                        dtValue.setAttributeNode(attr)
                        txt = sddXml.createTextNode("W. Australia Standard Time")
                        dtValue.appendChild(txt)
                        dtPropSet.appendChild(dtValue)
                        propArray.appendChild(dtPropSet)

                    #loop through the property array
                    for propSet in propArray.childNodes:

                        #loop through the property set
                        for prop in propSet.childNodes:
                            if prop.tagName == "Key":
                                if prop.firstChild.data == "WebCapabilities":
                                    prop.nextSibling.firstChild.data = "Map,Query,Data"
                                elif prop.firstChild.data == "maxRecordCount":
                                    prop.nextSibling.firstChild.data = f"{maxRecords}"
                                elif prop.firstChild.data in ["MinInstances", "MaxInstances"]:
                                    prop.nextSibling.firstChild.data = f"{minMax}"
                                elif prop.firstChild.data == "provider":
                                    prop.nextSibling.firstChild.data = "DMaps" if instanceType == "SHARED" else "ArcObjects11"
    
    #set the feature service settings
    for typeNameNode in typeNameNodes:
        if typeNameNode.firstChild.data != "FeatureServer":
            continue

        utils.logAndNotify(logger, "Setting FEATURE SERVER settings")

        #get the parent node
        parentNode = typeNameNode.parentNode
        
        #loop through the first child
        for node1 in parentNode.childNodes:
            if node1.tagName not in ["Info", "Enabled", "Props"]:
                continue
            
            #enable the setting
            if node1.tagName == "Enabled":
                node1.firstChild.data = "true"
                continue

            #loop through third child - property array
            for propArray in node1.childNodes:

                #loop through the property array
                for propSet in propArray.childNodes:

                    #loop through the property set
                    for prop in propSet.childNodes:
                        if prop.tagName == "Key":
                            if prop.firstChild.data == "WebCapabilities":
                                prop.nextSibling.firstChild.data = "Query,Sync,Extract"
                            elif prop.firstChild.data == "maxRecordCount":
                                prop.nextSibling.firstChild.data = f"{maxRecords}"

    #output the values
    with open(sddFile, "w") as f:
        sddXml.writexml(f)
    
    #stage service
    utils.logAndNotify(logger, "Staging service")
    arcpy.server.StageService(sddFile, sdFile)

    #share
    utils.logAndNotify(logger, "Pushing to Portal")
    arcpy.server.UploadServiceDefinition(sdFile, svrDict[db]['server'])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 22 Oct 2023 11:51:51 GMT</pubDate>
    <dc:creator>MatthewGeorge</dc:creator>
    <dc:date>2023-10-22T11:51:51Z</dc:date>
    <item>
      <title>Publish a Map Image Layer with Feature Service Enabled - Feature Layer Not Nested - Using arcpy</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-map-image-layer-with-feature-service/m-p/1339929#M69266</link>
      <description>&lt;P&gt;Is it possible to publish a single layer programmatically so that it is not nested in it's Feature Service. When I publish a layer using the below script and I add the resulting Feature Service to Pro from Portal, it adds as a nested layer - My Layer\My Layer - but if you publish a Layer using the normal workflow from Pro by right clicking the Layer in the contents pane and selecting Sharing &amp;gt; Share As Web Layer, adding it to the project will only add the inner layer, not the nested layer.&lt;/P&gt;&lt;P&gt;Not sure if that makes sense but can try explain more clearly if required. Following screenshots might help to clarify as well.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MatthewGeorge_0-1697975459039.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/83654iF98C0600EBF1EE7D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MatthewGeorge_0-1697975459039.png" alt="MatthewGeorge_0-1697975459039.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm using the following code to do the publishing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    aprx = arcpy.mp.ArcGISProject("CURRENT")
    m = aprx.activeMap

    #check that the map is valid
    if m == None:
        utils.logAndNotify(logger, "No Map is currently active", "ERROR")
        return
    
    #get the layer
    lyr = m.listLayers(lyrName)[0]

    #get and set the values in the SD draft
    utils.logAndNotify(logger, "Setting values to use in the SD draft")
    sdd = m.getWebLayerSharingDraft("FEDERATED_SERVER", "MAP_IMAGE", lyrName, lyr)
    sdd.federatedServerUrl = svrDict[db]['server']
    sdd.description = desc
    sdd.tags = tags
    sdd.serverFolder = svrDict[db]['folder']

    #export to SD draft file
    utils.logAndNotify(logger, "Exporting to file")
    sdd.exportToSDDraft(sddFile)

    #read the output xml file
    utils.logAndNotify(logger, "Updating the SDD XML file")
    sddXml = md.parse(sddFile)
    xmlKeys = sddXml.getElementsByTagName("Key")
    xmlValues = sddXml.getElementsByTagName("Value")

    #set sharing settings
    everyone = "false"
    group = "false"

    #set values required
    for i in range(xmlKeys.length):
        if xmlKeys[i].firstChild.nodeValue == "PackageUnderMyOrg":
            xmlValues[i].firstChild.nodeValue = org
        if xmlKeys[i].firstChild.nodeValue == "PackageIsPublic":
            xmlValues[i].firstChild.nodeValue = everyone
        if xmlKeys[i].firstChild.nodeValue == "PackageShareGroups":
            xmlValues[i].firstChild.nodeValue = group
        if xmlKeys[i].firstChild.nodeValue == "CallingContext":
            xmlValues[i].firstChild.nodeValue = "0"
        if xmlKeys[i].firstChild.nodeValue == "useMapServiceLayerID":
            xmlValues[i].firstChild.nodeValue = "true"

    #get the type name nodes
    typeNameNodes = sddXml.getElementsByTagName("TypeName")
    for typeNameNode in typeNameNodes:
        if typeNameNode.firstChild.data != "MapServer":
            continue

        utils.logAndNotify(logger, "Setting MAP SERVER settings")

        #get the parent node
        parentNode = typeNameNode.parentNode
        
        #loop through the first child
        for node1 in parentNode.childNodes:
            if node1.tagName != "Definition":
                continue

            #loop through the second child
            for node2 in node1.childNodes:
                if node2.tagName not in ["Info", "ConfigurationProperties","Props"]:
                    continue

                #loop through third child - property array
                for propArray in node2.childNodes:
                    
                    #set the timezone details
                    if node2.tagName == "ConfigurationProperties":
                        dtPropSet = sddXml.createElement("PropertySetProperty")
                        attr = sddXml.createAttribute("xsi:type")
                        attr.value = "typens:PropertySetProperty"
                        dtPropSet.setAttributeNode(attr)
                        dtKey = sddXml.createElement("Key")
                        txt = sddXml.createTextNode("dateFieldsTimezoneID")
                        dtKey.appendChild(txt)
                        dtPropSet.appendChild(dtKey)
                        dtValue = sddXml.createElement("Value")
                        attr = sddXml.createAttribute("xsi:type")
                        attr.value = "xs:string"
                        dtValue.setAttributeNode(attr)
                        txt = sddXml.createTextNode("W. Australia Standard Time")
                        dtValue.appendChild(txt)
                        dtPropSet.appendChild(dtValue)
                        propArray.appendChild(dtPropSet)

                    #loop through the property array
                    for propSet in propArray.childNodes:

                        #loop through the property set
                        for prop in propSet.childNodes:
                            if prop.tagName == "Key":
                                if prop.firstChild.data == "WebCapabilities":
                                    prop.nextSibling.firstChild.data = "Map,Query,Data"
                                elif prop.firstChild.data == "maxRecordCount":
                                    prop.nextSibling.firstChild.data = f"{maxRecords}"
                                elif prop.firstChild.data in ["MinInstances", "MaxInstances"]:
                                    prop.nextSibling.firstChild.data = f"{minMax}"
                                elif prop.firstChild.data == "provider":
                                    prop.nextSibling.firstChild.data = "DMaps" if instanceType == "SHARED" else "ArcObjects11"
    
    #set the feature service settings
    for typeNameNode in typeNameNodes:
        if typeNameNode.firstChild.data != "FeatureServer":
            continue

        utils.logAndNotify(logger, "Setting FEATURE SERVER settings")

        #get the parent node
        parentNode = typeNameNode.parentNode
        
        #loop through the first child
        for node1 in parentNode.childNodes:
            if node1.tagName not in ["Info", "Enabled", "Props"]:
                continue
            
            #enable the setting
            if node1.tagName == "Enabled":
                node1.firstChild.data = "true"
                continue

            #loop through third child - property array
            for propArray in node1.childNodes:

                #loop through the property array
                for propSet in propArray.childNodes:

                    #loop through the property set
                    for prop in propSet.childNodes:
                        if prop.tagName == "Key":
                            if prop.firstChild.data == "WebCapabilities":
                                prop.nextSibling.firstChild.data = "Query,Sync,Extract"
                            elif prop.firstChild.data == "maxRecordCount":
                                prop.nextSibling.firstChild.data = f"{maxRecords}"

    #output the values
    with open(sddFile, "w") as f:
        sddXml.writexml(f)
    
    #stage service
    utils.logAndNotify(logger, "Staging service")
    arcpy.server.StageService(sddFile, sdFile)

    #share
    utils.logAndNotify(logger, "Pushing to Portal")
    arcpy.server.UploadServiceDefinition(sdFile, svrDict[db]['server'])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2023 11:51:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-map-image-layer-with-feature-service/m-p/1339929#M69266</guid>
      <dc:creator>MatthewGeorge</dc:creator>
      <dc:date>2023-10-22T11:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Publish a Map Image Layer with Feature Service Enabled - Feature Layer Not Nested - Using arcpy</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-map-image-layer-with-feature-service/m-p/1495853#M70896</link>
      <description>&lt;P&gt;I've encountered the same problem, did you ever find a solution?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 12:45:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-map-image-layer-with-feature-service/m-p/1495853#M70896</guid>
      <dc:creator>Chris_Matthews</dc:creator>
      <dc:date>2024-06-21T12:45:55Z</dc:date>
    </item>
  </channel>
</rss>

