Trouble updating feature service capabilities through Python

223
6
Friday
JarrettBraden
Emerging Contributor

My county is currently setting up a new Enterprise geodatabase. All the data is hosted locally, and then a data store creates the feature and map services in bulk in our Enterprise Portal. I want to use a python script to automatically update service cababilties for all our services since it's time consuming to go through and set capabilites one by one. 

I'm currently using the following script,

from arcgis.gis import GIS 
from arcgis.features import FeatureLayerCollection 
gis = GIS("https://www.arcgis.com", "username", "password" verify_cert = False) 
url = url = "https://your/server/url/FeatureServer" 
flc = FeatureLayerCollection(url, gis=gis) 
update_dict = { "hasStaticData": False, "capabilities": "Query,Create,Update,Delete,Editing,ChangeTracking,Append"} 
flc.manager.update_definition(update_dict)

 I get the following error,

Exception: {'code': 500, 'message': "Service 'name'.'FeatureServer' does not exist in folder 'name'.", 'details': []}

 I've tried following the steps from;

this How To https://support.esri.com/en-us/knowledge-base/how-to-change-the-capabilities-of-a-hosted-feature-ser... 

this thread, https://community.esri.com/t5/python-questions/enable-editing-amp-editor-tracking-via-python-api/td-... 

and the API documentation here https://developers.arcgis.com/python/latest/guide/updating-feature-layer-properties/ 

to no success.  

The service I want to update is in the folder. I can see it on Server Manager and in the ArcGIS REST Services Directory. I'm stumped, any help would be appreciated!

Tags (3)
0 Kudos
6 Replies
TonyAlmeida
MVP Regular Contributor

The error suggests the script can't find your service at the specified location. You're using ArcGIS Online credentials but your services are hosted locally.

Connect to  Enterprise Porta and Verify the connection

 

 

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

try:
    # Connect to Enterprise Portal
    gis = GIS("https://yourportal.domain.com/portal", "username", "password", verify_cert=False)
    print(f"Connected to: {gis.properties.portalName}")
    print(f"Logged in as: {gis.users.me.username}")
    
    # Update service capabilities
    service_url = "https://yourserver/rest/services/YourService/FeatureServer"
    flc = FeatureLayerCollection(service_url, gis=gis)
    flc.manager.update_definition({
        "hasStaticData": False,
        "capabilities": "Query,Create,Update,Delete,Editing,ChangeTracking,Append"
    })
    print("Service capabilities updated successfully")

except Exception as e:
    print(f"\nError: {str(e)}")
    print("\nTips:")
    print("- Verify the service URL in your REST Services Directory")
    print("- Check if your user has administrator privileges")
    print("- Ensure the service exists in the specified folder")

 

0 Kudos
JarrettBraden
Emerging Contributor

Tony,

I have verified my connection to Enterprise Portal and followed your code example exactly. 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

try:
        # Connect to Enterprise Portal
        gis = GIS("https://MyEnterprise/portal", "Username", 'Password', verify_cert=False)
        print(f"Connected to: {gis.properties.portalName}")
        print(f"Logged in as: {gis.users.me.username}")
        
        # Update service capabilities
        service_url = "https://MyEnterprise/server/rest/services/FolderName/ServiceName/FeatureServer/"
        flc = FeatureLayerCollection(service_url, gis=gis)
        print(flc.properties)
        flc.manager.update_definition({
            "hasStaticData": False,
            "capabilities": "Query,Create,Update,Delete,Editing,ChangeTracking,Append"
        })
        print("Service capabilities updated successfully")

To the same result,

Error: {'code': 500, 'message': "Service 'ServiceName'.'FeatureServer' does not exist in folder 'FolderName'.", 'details': []}

I've tested it with multiple service URL's to the same result. I added in the print(flc.properties) to verify that my connection works and that I can see the properties of my sercive, and it sucessfully lists out all the properties.

The username and password is the account that owns the portal data store used to publish the layers in bulk, and it has the max permissions it can be given in Portal. I've also been able to the following code to verify that all the services I want to update are in the correct folder, and it also returns everything sucessfully.

gis = GIS("https://MyEnterprise/portal", "Username", 'Password', verify_cert=False)
    
user = gis.users.get("Username")
folder_items = user.items("FolderName",max_items=1000)
for item in folder_items:
    print(f"Item Title: {item.title}, Item Type: {item.type}, Item ID: {item.id}, Item URL: {item.url}")

If it is a privileges issue, I'm not sure what needs to be updated. 

 

0 Kudos
TonyAlmeida
MVP Regular Contributor

The following works for me, my guess is service url formatting issue.

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# 1. Connect to your Enterprise Portal
portal_url = "https://arcgisportal"  # Replace with your portal URL
username = "Username"  # Replace with your username
password = "Password"  # Replace with your password

try:
    # Connect to the portal
    gis = GIS(portal_url, username, password)
    print("Successfully connected to: {}".format(gis.properties.portalName))
    print("Logged in as: {}".format(gis.properties.user.username))
    
    # 2. Verify a service URL
    service_url = "https://MyEnterprise/arcgisserver/rest/services/FolderName/ServiceName/FeatureServer"  # Replace with your service URL
    
    try:
        # Create a FeatureLayer object
        feature_layer = FeatureLayer(service_url, gis)
        
        # Check if the layer is queryable
        query_result = feature_layer.query(where="1=1", return_count_only=True)
        print(f"Service is working! Found {query_result} features in the layer.")
        
        # Optional: Print some layer properties
        print("\nLayer properties:")
        print(f"Name: {feature_layer.properties.name}")
        print(f"Type: {feature_layer.properties.type}")
        print(f"Geometry Type: {feature_layer.properties.geometryType}")
        
    except Exception as e:
        print(f"Error accessing service: {str(e)}")
        print(f"The service URL {service_url} might not be working or accessible.")
        
except Exception as e:
    print(f"Error connecting to portal: {str(e)}")

 

0 Kudos
JarrettBraden
Emerging Contributor

I'm copying the URL's straight from my ArcGIS REST Services Directory. Two new results using the new script. 

#Keeping the URL the same
service_url = "https://MyEnterprise/server/rest/services/FolderName/ServiceName/FeatureServer"

#That outputs
Successfully connected to: ArcGIS Enterprise
Logged in as: Username
Error accessing service: 'count'
The service URL https://MyEnterprise/server/rest/services/FolderName/ServiceName/FeatureServer/ might not be working or accessible.
#Adding the "/0" to the URL
service_url = "https://MyEnterprise/server/rest/services/FolderName/ServiceName/FeatureServer/0"

#That outputs
Successfully connected to: ArcGIS Enterprise
Logged in as: Username
Service is working! Found 307 features in the layer.

Layer properties:
Name: ServiceName
Type: Feature Layer
Geometry Type: esriGeometryPolygon

However, when I use the "/0" URL in the FeatureLayerCollection example from before, it give me the following error instead of the Error 500.

Connected to: ArcGIS Enterprise
Logged in as: Username
Error: 'PropertyMap' instance has no attribute 'layers'
0 Kudos
TonyAlmeida
MVP Regular Contributor

You get 500 error,  because FeatureLayerCollection expects a service-level URL, no /0.

if you can't use item IDs or metadata due to your security restrictions by your admin

Item URL: Points to the Portal item (https://portal/...)

Service URL: Points to the actual REST endpoint (https://server/...)

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS("https://MyEnterprise/portal", "Username", "Password", verify_cert=False)

# Get all feature services in the folder
items = gis.content.search("owner:Username folder:FolderName", item_type="Feature Service")

for item in items:
    try:
        print(f"\nProcessing: {item.title}")
        
        # Get the actual service URL from the item's layers property
        if not item.layers:  # Some feature services use tables instead
            service_url = item.tables[0].url
        else:
            service_url = item.layers[0].url
            
        print(f"Service URL: {service_url}")
        
        # Connect to the actual service endpoint
        flc = FeatureLayerCollection(service_url, gis)
        
        # Update capabilities
        flc.manager.update_definition({
            "hasStaticData": False,
            "capabilities": "Query,Create,Update,Delete,Editing,ChangeTracking,Append"
        })
        
        print("Successfully updated capabilities")
        
    except Exception as e:
        print(f"Failed to update {item.title}: {str(e)}")

 

0 Kudos
JarrettBraden
Emerging Contributor

Using that script, it ran and finished as if it was successful, but nothing ended up changing. All service cababilites are still default. I changed the line below,

items = gis.content.search("owner:Username folder:FolderName", item_type="Feature Service")

 to the following.

user = gis.users.get("Username")
folder_items = user.items("FolderName",max_items=1000)

 That ended up looping through my entire folder, giving me the following output for each service.

Processing: ServiceName
Service URL: https://MyEnterprise/server/rest/services/FolderName/ServiceName/FeatureServer/0
Failed to update ServiceName: 'PropertyMap' instance has no attribute 'layers'

Based on your previous reply, we don't want it returning and using the "/0" URL. 

It seems like we're close, but something still isn't right. Unfortunately, I'm not able to contact my Server/Portal/Database Admin as they're out this week. If some security change needs made, I will have to wait until next week before anything gets done. 

0 Kudos