How to Overwrite ArcGIS Server (on-premise) Feature Service with Python?

4990
4
04-02-2015 11:29 AM
MicahBabinski
Occasional Contributor III

Greetings,

I am working on a script tool that will overwrite an existing service, with the option to identify it as a feature service. I'm using ArcGIS 10.2.0. When I run the script (abstracted and included below) with the feature service option activated, I consistently get an error of:

The script tool takes three parameters:

  1. Service Folder - Rest Endpoint (String)
  2. Service Name (String)
  3. Is Feature Service (bool)

The workflow I am following is

  1. Create Map SD draft
  2. Update the SVCManifest/TypeName portion of the SD draft xml to be "esriServiceDefinitionType_replacement"
  3. If the feature service option is selected, modify the XML to specify TypeName of FeatureServer, State of esriSDState_Published, turn off caching, and grant full web capabilities including Query,Create,Update,Delete,Uploads,Editing
  4. Save the updated xml to the disk
  5. Stage the service
  6. Upload the service definition if there are no errors

The script is attached. Based on my research so far, my best guess is that my modifications to the XML are somehow invalidating it. Here is that specific portion:

# Identify the SD draft to use
if str(is_feature_service) == "true":
    xml = newSDdraft
else:
    xml = sddraft


# Set service type to esriServiceDefinitionType_Replacement
newType = 'esriServiceDefinitionType_Replacement'
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
    if desc.parentNode.tagName == 'SVCManifest':
        if desc.hasChildNodes():
            desc.firstChild.data = newType


# If feature service is selected, change the service type from map service to feature service
if str(is_feature_service) == "true":
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        if typeName.firstChild.data == "MapServer":
            typeName.firstChild.data = "FeatureServer"


    tagsState = doc.getElementsByTagName('State')
    for tagState in tagsState:
        if tagState.parentNode.tagName == 'SVCManifest':
            if tagState.hasChildNodes():
                tagState.firstChild.data = "esriSDState_Published"


    # ...turns off caching
    configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
    propArray = configProps.firstChild
    propSets = propArray.childNodes
    for propSet in propSets:
        keyValues = propSet.childNodes
        for keyValue in keyValues:
            if keyValue.tagName == 'Key':
                if keyValue.firstChild.data == "isCached":
                    keyValue.nextSibling.firstChild.data = "false"


    # ...and turn on feature access capabilities
    configProps = doc.getElementsByTagName('Info')[0]
    propArray = configProps.firstChild
    propSets = propArray.childNodes
    for propSet in propSets:
        keyValues = propSet.childNodes
        for keyValue in keyValues:
            if keyValue.tagName == 'Key':
                if keyValue.firstChild.data == "WebCapabilities":
                    keyValue.nextSibling.firstChild.data = "Query,Create,Update,Delete,Uploads,Editing"


    # Write the new draft to disk
    f = open(newSDdraft, 'w')
    doc.writexml( f )
    f.close()

The full script is attached. Any guesses or help would be much appreciated! Most of the help for overwriting a feature service that I found was for hosted feature services, so I'm a bit at a loss.

Regards,

Micah Babinski

0 Kudos
4 Replies
AndrewBrown
New Contributor III

A couple of things.

1.  Are you able to get any results during the analysis portion of staging the service and do they indicate any warnings/errors?

2.  Are you sure that the underlying data is supported for a feature service.

This section of the ArcGIS Help gives more details on the feature service requirements...

http://resources.arcgis.com/en/help/main/10.2/0154/0154000003nt000000.htm

MicahBabinski
Occasional Contributor III

Hi Andrew,

I should have specified that I have not had any problems overwriting this feature service using the GUI-based workflow within arcmap:

My interest is really to be able to overwrite these types of services automatically for when I have to make bulk updates to layer and MXD descriptions, symbology, or other characteristics of the MXD.

Thanks,

Micah

0 Kudos
ToddHenry1
Occasional Contributor

Micah,

The help documentation about changing to a feature service appears to be applicable to ArcGIS Online hosted services.  I got the following to work for ArcGIS Server:

#Enable FeatureServer
soe = 'FeatureServer'

typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
if typeName.firstChild.data == soe:
  extension = typeName.parentNode
  for extElement in extension.childNodes:
   if extElement.tagName =='Enabled':
    extElement.firstChild.data ='true'

#Enable overwrite existing service
tagsType = doc.getElementsByTagName('Type')
for tagType in tagsType:
    if tagType.parentNode.tagName == 'SVCManifest':
        if tagType.hasChildNodes():
            tagType.firstChild.data = "esriServiceDefinitionType_Replacement"

tagsState = doc.getElementsByTagName('State')
for tagState in tagsState:
    if tagState.parentNode.tagName == 'SVCManifest':
        if tagState.hasChildNodes():
            tagState.firstChild.data = "esriSDState_Published"

# Turn off Allow Geometry Updates
configProps = doc.getElementsByTagName('Props')[0]
propArray = configProps.firstChild
propSets = propArray.childNodes
for propSet in propSets:
    keyValues = propSet.childNodes
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if keyValue.firstChild.data == "allowGeometryUpdates":
                keyValue.nextSibling.firstChild.data = "False"

# Get all the value tags.
values = doc.getElementsByTagName('Value')
for value in values:
    if value.hasChildNodes():
        # Change the default WebCapabilities from 'Query,Create,Update,Delete,Uploads,Editing' to just 'Query'.
        if value.firstChild.data == 'Query,Create,Update,Delete,Uploads,Editing':
            value.firstChild.data = 'Query,Update,Uploads,Editing'

                     

However, the Turn off Allow Geometry Updates does not work.  I can change it manually  in the sddraft file, but that code does not edit the XML. Do you have any thoughts on what part should be changed to find the allowGeometryUpdates property.

0 Kudos
ToddHenry1
Occasional Contributor

I figured out the code to change the allow geometry updates.

This line configProps = doc.getElementsByTagName('Props')[0]
need to be configProps = doc.getElementsByTagName('Props')[1] because the Props
tag exists twice in the XML file.

0 Kudos