Select to view content in your preferred language

Not Able to Get Service Layers From ArcGIS API for Python

923
8
Jump to solution
05-16-2024 12:10 PM
BHK
by
Occasional Contributor

I am trying to get list of Layers from a Service using this code

 

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

gis_servers = gis.admin.servers.list()
server1 = gis_servers[0]
fservices = server1.services.list(folder='GDPServices')

for service in fservices:
        layer = FeatureLayerCollection(service.url)
        print(layer)

 

but I am  receiving this error message 

 

AttributeError: 'PropertyMap' instance has no attribute 'layers'

 

Can you please let me know why this is happening and how I can fix this?

0 Kudos
1 Solution

Accepted Solutions
EarlMedina
Esri Regular Contributor

The problem here is that you're getting the admin url for those services (which is not what you want to be using). They come back in the format: https://server.domain.com/server/admin/services/serviceName.MapServer

If you're not doing any Server administration then it would be better to just use gis.content, I'd say.

However, if you must stick with the Server submodule, then I think you can do so by changing your code to:

 

 

server1 = gis_servers[0]
fservices = server1.content.list(folder='GDPServices')

 

 

 

There is a catch - content.list doesn't have filtering built in beyond the folder and will try to initialize objects (this may be a slow process) from the services for you. So, unless you are certain all you have in there are Feature Services, I would recommend setting the as_dict param to True and getting only what  you need:

server.content.list(folder="Hosted", as_dict=True)

 

This adds extra work as you will need to filter for feature services and then concatenate the expected urls from the services directory url and content urls. So again, unless there's a reason you need to be using the Server submodule, probably using gis.content is going to be easier for whatever you're doing.

View solution in original post

0 Kudos
8 Replies
EarlMedina
Esri Regular Contributor

The problem here is that you're getting the admin url for those services (which is not what you want to be using). They come back in the format: https://server.domain.com/server/admin/services/serviceName.MapServer

If you're not doing any Server administration then it would be better to just use gis.content, I'd say.

However, if you must stick with the Server submodule, then I think you can do so by changing your code to:

 

 

server1 = gis_servers[0]
fservices = server1.content.list(folder='GDPServices')

 

 

 

There is a catch - content.list doesn't have filtering built in beyond the folder and will try to initialize objects (this may be a slow process) from the services for you. So, unless you are certain all you have in there are Feature Services, I would recommend setting the as_dict param to True and getting only what  you need:

server.content.list(folder="Hosted", as_dict=True)

 

This adds extra work as you will need to filter for feature services and then concatenate the expected urls from the services directory url and content urls. So again, unless there's a reason you need to be using the Server submodule, probably using gis.content is going to be easier for whatever you're doing.

0 Kudos
BHK
by
Occasional Contributor

Thanks @EarlMedina 

It is working now but when trying this server.content.list(folder="GDPService", as_dict=True)

I am getting  this error

 

TypeError: list() got an unexpected keyword argument 'as_dict'

 

0 Kudos
EarlMedina
Esri Regular Contributor

Might be a version problem? I am on 2.3.0.1 and it works fine.

 

0 Kudos
BHK
by
Occasional Contributor

and this 2.3.0.1 is version of what?

0 Kudos
EarlMedina
Esri Regular Contributor

sorry, the version of the API.

0 Kudos
BHK
by
Occasional Contributor

Very silly question but where can I find the in used API version? is it the one compatible with Pro version? 

0 Kudos
EarlMedina
Esri Regular Contributor

There's a few ways you can do this. You can either check the Pro Package Manager (arcgis), run "conda list" and check the arcgis package version, or in code like this:

 

import arcgis
print(arcgis.__version__)

 

 

0 Kudos
BHK
by
Occasional Contributor

Thanks a lost @EarlMedina 

My version then is 1.8.3 and maybe that why I was getting that error

By the way, can I ask you to kindly take a look at this Post too. I 

Issue on Listing Service Service Workspaces Inform... - Esri Community

 

I need to get the service workspace information but not shore it is doable by ArcGIS API for Python. Thanks again

 

0 Kudos