Select to view content in your preferred language

How to query a specific layer from a Feature Service

01-26-2022 08:07 AM
New Contributor III

Hello everyone,
I am trying to get the list of feature service which has a specific layer (for example WETTBEWERBER (the layer id is 0)) from a feature service,


from arcgis.gis import GIS
gis = GIS(url='https://url/portal', username='arcgis_python', password='amazing_arcgis_123')

layer_items ="WETTBEWERBER", item_type="Feature Layer")
for layer in layer_items:
    if "WETTBEWERBER" in layer.title.upper():
        print(f"Layer name: {layer.title}, \tOwner: {layer.owner}, \tLayer ID: {}")

 But no results are printed out, but the layer WETTBEWERBER is present in most of my feature services. What can be done in this case ??

I have tried to get the layers first using the following code to check whether I can get the layers using the API and I can get the layers. 


layer_items ="5fg239sdsfs56sdfcs466s4f", item_type="Feature Service")

for item in self.api_query_result:
    layers = item.layers
for lyr in layers:
except Exception as e:

I can get all the layers including the layer WETTBEWERBER (0) in it. 

But I cannot list all the feature service which contains this particular layer, What am I doing wrong ?? 

Thank you for the insights. 

0 Kudos
4 Replies
MVP Esteemed Contributor

layer.title.lower() would not match an all-caps string like "WETTBEWERBER". Did you mean "layer.title.upper()"?

- Josh Carlson
Kendall County GIS
New Contributor III

Yes, sorry for the typo here it is "layer.title.upper()" I have updated the post. 

0 Kudos
MVP Esteemed Contributor

Looking at it again, a layer doesn't have a title property. The parent item has a title, but the layers have properties, in which you can find a name, as shown in the other code block you quoted.

To get the feature service itself, you have to reference the parent item. This snippet would return a list of all Feature Layers in your search:


items ='some string', item_type='Feature Service')

[a.url for b in items for a in b.layers]


 To filter this list, we can include a simple ternary operator in the same line.


[a for b in items for a in b.layers if['name'] == 'WETTBEWERBER']


I tested this on my own layers, and searching for the layer name "Records" across a bunch of layers yielded the following:

- Josh Carlson
Kendall County GIS
New Contributor III

Thank you for the detailed explanation, whether I should leave the query empty in the place of "some string" and kindly can you edit it with the exact syntax I cannot figure out from this, that will be really helpful. 

0 Kudos