I am using ArcGIS Pro 2.9.2 (the latest at this writing). My project's Python Notebook reports that my sys.version is 3.7.11.
My goal is to create a function that tells me which Portal items are using a given ArcGIS Server service, and which Portal items are used by those items, ad infinitum. For example, if I query Portal for a URL to my asphalt service, I want to see the map layers, feature layers, map packages, maps, dashboards, that depend on it
My problem is that a basic Python function is throwing an exception when it should just be returning False.
I am looping through the results of arcgis.gis.GIS.content.search. This returns a list of objects of type arcgis.gis.Item. The documentation for that class says, "Items that have layers (eg FeatureLayerCollection items and ImageryLayer items) and tables have the dynamic layers and tables properties to get to the individual layers/tables in this item."
When I call hasattr(webItem, "layers") on an Item that has a layers property, the hasattr function returns True. However, when I call this function on an Item that doesn't have a layers property, a KeyError exception is thrown. The same occurs if I call getattr(webItem, "layers", ). What's interesting to note is that if I call hasattr(webItem, "abc"), then False is returned.
Why doesn't hasattr just return False, and getattr just return an empty list, when a property named layers doesn't exist on a web Item?
I'm also open to different ways of crawling through Portal to achieve the goal stated above.