The list of feature layers containing specific layers are not listed.

275
0
01-25-2022 08:10 AM
Aravinthkumar
New Contributor III

Hello, 

I have many feature service in my portal which contains 4 to 6 layers in each of them, I wanted to list all the feature service which contain a specific layer called WETTBEWER which is layer (0) in all the feature services. I have the following code, but nothing is returned in the results, What Am I doing wrong ?? The specific process is done in the json_writer.py

**config.json**

{
"url": "https://url/portal/",
"username": "user",
"password":"pass",
"query": "title: WETTBEWER",
"item_type": "Feature Service",
"max_items": 400
}

**json_reader.py**

import json

class JsonReader:

        def __init__(self, filename):
               self.filename = filename

       def read_json_file(self):
              with open(self.filename) as file:
                     data = json.load(file)
             return data

      def get_config(self, config):
             loaded_json = self.read_json_file()
             config = loaded_json[config]
             return config

**portal_connection.py**

from arcgis.gis import GIS

class PortalConnection:
        def __init__(self, url, username, password):
              self.url = url
              self.username = username
              self.password = password

def connect(self, query, layer, max_items):
# Connection to ArcGIS Enterprise using a built-in account
       print("Portal for ArcGIS as a built in user")
      gis_portal = GIS(self.url, self.username, self.password)
      print("Logged in as: " + gis_portal.properties.user.username)
      return gis_portal, query, layer, max_items

**Esri_api.py**

class EsriApi:

         def __init__(self, portal, item_type, query, max_items):
                self.item_type = item_type
                self.query_ = query
                self.portal = portal
                self.max_items = max_items

       def query(self):
              api_query_result = self.portal.content.search(query=self.query_,                    item_type=self.item_type, max_items=self.max_items)
              return api_query_result

**json_writer.py**

class JsonWriter:

         def __init__(self,api_query_result):
               self.api_query_result = api_query_result

         def printResults(self):

               for item in self.api_query_result:
                      layers = item.layers
                      for lyr in layers:
                            try:
                                 print(item.id, item.url, lyr.properties["id"],                       lyr.properties["name"])
                            except Exception as e:
                                print(e)

**Main.py**

from json_reader import JsonReader
from portal_connection import PortalConnection
from esri_api import EsriApi
from json_writer import JsonWriter

jreader = JsonReader('config.json')
url = jreader.get_config("url")
username = jreader.get_config("username")
password = jreader.get_config("password")
query = jreader.get_config("query")
layer = jreader.get_config("item_type")
max_items = jreader.get_config("max_items")

con = PortalConnection(url, username, password)
portal, query, layer, max_items = con.connect(query, layer, max_items)

esri_api = EsriApi(portal, layer, query, max_items)
results = esri_api.query()

jsonWriter = JsonWriter(results)
jsonWriter.printResults()

 

Thank you for the insights...

0 Kudos
0 Replies