<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Trying to list all Feature Services in Portal and what Maps use those specific services in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1561567#M73232</link>
    <description>&lt;P&gt;Very useful script. I had a go at modifying it further to return the layers item ID and spatial reference as well, but failed. Anyone have any idea's on how to do that?&lt;/P&gt;</description>
    <pubDate>Fri, 22 Nov 2024 01:14:04 GMT</pubDate>
    <dc:creator>LindsayRaabe_FPCWA</dc:creator>
    <dc:date>2024-11-22T01:14:04Z</dc:date>
    <item>
      <title>Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242128#M66294</link>
      <description>&lt;P&gt;So I am trying to create an excel document that list all of my feature services / feature collections&amp;nbsp; and what specific maps they are in, so far I have this, but when it runs it lists all my services in Column A, but then Column B is blank, any idea what could be happening?&amp;nbsp; I am very new to python.&amp;nbsp; Any help would be appreciated.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcgis
import openpyxl

# Set the URL of your ArcGIS Online portal
portal_url = 'https://yourportal.arcgis.com'

# Set your ArcGIS Online username and password
username = 'your_username'
password = 'your_password'

# Connect to the portal
gis = arcgis.GIS(portal_url, username, password)

# Search for all feature services and feature collections in the portal
items = gis.content.search(query='type: "Feature Service" OR type: "Feature Collection"', max_items=5000)

# Create a new Excel workbook
workbook = openpyxl.Workbook()
worksheet = workbook.active

# Add a header row to the worksheet
worksheet.append(['Feature Service/Collection', 'Maps Using Feature'])

# Iterate through the feature services and feature collections
for item in items:
    # Search for maps in the portal that use the feature service or feature collection as a layer
    maps = gis.content.search(query=f'type: "Web Map*" AND layers: {item.id}', max_items=5000)
    
    # Create a list of the map names
    map_names = [map.title for map in maps]
    
    # Add a row to the worksheet with the feature service/collection name and the list of map names
    worksheet.append([item.title, *map_names])

# Save the workbook to a file
workbook.save('feature_services_maps.xlsx')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2022 20:08:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242128#M66294</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-12-16T20:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242139#M66295</link>
      <description>&lt;P&gt;When I tested this, I was getting an empty empty list when I printed the map_names list.&amp;nbsp; Are you sure it is populating?&amp;nbsp; I would try building in some print statements to test your results.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2022 20:46:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242139#M66295</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2022-12-16T20:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242148#M66297</link>
      <description>&lt;P&gt;&lt;SPAN&gt;This is what the output looks like for me, I am using Enterprise 10.8.1, it seems like it is picking up more than just services but the built in esri tools as well.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="phess_luckstone_1-1671224359589.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/58800i5673E57159313F06/image-size/medium?v=v2&amp;amp;px=400" role="button" title="phess_luckstone_1-1671224359589.png" alt="phess_luckstone_1-1671224359589.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2022 20:59:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242148#M66297</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-12-16T20:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242151#M66298</link>
      <description>&lt;P&gt;your map_names variable is trying to populate a list with map.title, and it errors out telling me that map has no attribute 'title'&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2022 21:10:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1242151#M66298</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2022-12-16T21:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1243176#M66328</link>
      <description>&lt;P&gt;As I think it's more reliable that way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;I'm also not 100% sure you can query a webmap on its layers using the gis.content.search function. I wrote some code to search webmaps for all services in a portal/agol instance to find services that were unused, which you can find here&amp;nbsp;&lt;A href="https://github.com/joshsharpheward/gis-administration/blob/master/find_unused_services.py" target="_blank"&gt;https://github.com/joshsharpheward/gis-administration/blob/master/find_unused_services.py&lt;/A&gt;&amp;nbsp;which I think you could modify to do what you need or at least use for inspiration!&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2022 02:36:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1243176#M66328</guid>
      <dc:creator>JoshuaSharp-Heward</dc:creator>
      <dc:date>2022-12-22T02:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1243315#M66333</link>
      <description>&lt;P&gt;So modifying a little of what you your code is, I was able to get the output of every map and every service inside of the map using this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.mapping import WebMap
import arcpy
import pandas as pd

def main():
    # logs into active portal in ArcGIS Pro
    gis = GIS('pro')

    arcpy.AddMessage("Logged into {} as {}".format(arcpy.GetActivePortalURL(), gis.properties['user']['username']))

    # creates list of items of all map image, feature, vector tile and image services (up to 10000 of each) in active portal
    services = (gis.content.search(query="", item_type="Map Service", max_items=10000) +
                gis.content.search(query="", item_type="Feature Service", max_items=10000) +
                gis.content.search(query="", item_type="Vector Tile Service", max_items=10000) +
                gis.content.search(query="", item_type="Image Service", max_items=10000))

    arcpy.AddMessage('Searching webmaps in {}'.format(arcpy.GetActivePortalURL()))

    # creates list of items of all webmaps in active portal
    web_maps = gis.content.search(query="", item_type="Web Map", max_items = 10000)

    # Create an empty data frame to store the data
    df = pd.DataFrame(columns=['Web Map', 'Service URL'])

    # loops through list of webmap items
    for item in web_maps:
        # creates a WebMap object from input webmap item
        web_map = WebMap(item)
        # accesses basemap layer(s) in WebMap object
        basemaps = web_map.basemap['baseMapLayers']
        # accesses layers in WebMap object
        layers = web_map.layers

        # loops through basemap layers
        for bm in basemaps:
            # tests whether the bm layer has a styleUrl(VTS) or url (everything else)
            if 'styleUrl'in bm.keys():
                for service in services:
                    if service.url in bm['styleUrl']:
                        services.remove(service)
            elif 'url' in bm.keys():
                for service in services:
                    if service.url in bm['url']:
                        services.remove(service)

        # loops through layers
        for layer in layers:
            # Add a new row to the data frame for each service used in the web map
            if hasattr(layer, 'styleUrl'):
                df.loc[len(df)] = [item.title, layer.styleUrl]
            elif hasattr(layer, 'url'):
                df.loc[len(df)] = [item.title, layer.url]

    # Save the data frame to an Excel file
    df.to_excel('web_maps.xlsx', index=False)

if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 22 Dec 2022 14:27:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1243315#M66333</guid>
      <dc:creator>phess_luckstone</dc:creator>
      <dc:date>2022-12-22T14:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1561567#M73232</link>
      <description>&lt;P&gt;Very useful script. I had a go at modifying it further to return the layers item ID and spatial reference as well, but failed. Anyone have any idea's on how to do that?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2024 01:14:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1561567#M73232</guid>
      <dc:creator>LindsayRaabe_FPCWA</dc:creator>
      <dc:date>2024-11-22T01:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to list all Feature Services in Portal and what Maps use those specific services</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1571099#M73424</link>
      <description>&lt;P&gt;Note to other tinkerers. I tweaked the code as per below to use print statements instead of arcpy.AddMessage, but essentially is just the same, exporting an .xlsx with a list of webmaps and their feature services (as feature service URL's only). I then exported an item report from ArcGIS Online and used Vlookup using the service URL to get more information on each layer.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.mapping import WebMap
import arcpy
import pandas as pd

def main():
    # logs into active portal in ArcGIS Pro
    gis = GIS('pro')

    print("Logged into {} as {}".format(arcpy.GetActivePortalURL(), gis.properties['user']['username']))

    # creates list of items of all map image, feature, vector tile and image services (up to 10000 of each) in active portal
    services = (gis.content.search(query="", item_type="Map Service", max_items=10000) +
                gis.content.search(query="", item_type="Feature Service", max_items=10000) +
                gis.content.search(query="", item_type="Vector Tile Service", max_items=10000) +
                gis.content.search(query="", item_type="Image Service", max_items=10000))

    print('Searching webmaps in {}'.format(arcpy.GetActivePortalURL()))

    # creates list of items of all webmaps in active portal
    web_maps = gis.content.search(query="", item_type="Web Map", max_items = 10000)

    # Create an empty data frame to store the data
    df = pd.DataFrame(columns=['Web Map', 'Service URL'])

    # loops through list of webmap items
    for item in web_maps:
        # creates a WebMap object from input webmap item
        web_map = WebMap(item)
        # accesses basemap layer(s) in WebMap object
        basemaps = web_map.basemap['baseMapLayers']
        # accesses layers in WebMap object
        layers = web_map.layers

        # loops through basemap layers
        for bm in basemaps:
            # tests whether the bm layer has a styleUrl(VTS) or url (everything else)
            if 'styleUrl'in bm.keys():
                for service in services:
                    if service.url in bm['styleUrl']:
                        services.remove(service)
            elif 'url' in bm.keys():
                for service in services:
                    if service.url in bm['url']:
                        services.remove(service)

        # loops through layers
        for layer in layers:
            # Add a new row to the data frame for each service used in the web map
            if hasattr(layer, 'styleUrl'):
                df.loc[len(df)] = [item.title, layer.styleUrl]
            elif hasattr(layer, 'url'):
                df.loc[len(df)] = [item.title, layer.url]

    # Save the data frame to an Excel file
    df.to_excel(r'C:\temp\web_maps.xlsx', index=False)

    print("Finished")

if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then also played with the coding posted here (&lt;A href="https://community.esri.com/t5/arcgis-online-questions/where-s-the-spatial-reference-information/m-p/1290601/highlight/true#M52295" target="_blank"&gt;Solved: Re: Where's the spatial reference information? - Esri Community&lt;/A&gt;) to print a list of feature services in ArcGIS Online and their WKID's which I could copy/paste into excel and also perform a Vlookup to the item report from ArcGIS Online to get more info for each layer.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS

# Connect to AGOL
agol = GIS("home")

# Search for all items in AGOL
items = agol.content.search(query="", max_items=10000)

# Iterate through each item
for item in items:
    try:
        # If it is a feature service or vector tile package
        if item.type in ("Feature Service", "Vector Tile Package"):
            # Print the WKID of the SRS
            print("Item ID: {0}, WKID: {1}".format(item.id, item["spatialReference"]))
        else:
            print("Skipping Item ID: {0}, Type: {1}".format(item.id, item.type))

    except Exception as e:
        print("Failed to process item ID: {0}, Error: {1}".format(item.id, str(e)))&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LindsayRaabe_FPCWA_0-1735019171159.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/122380i326AC26AAA763927/image-size/large?v=v2&amp;amp;px=999" role="button" title="LindsayRaabe_FPCWA_0-1735019171159.png" alt="LindsayRaabe_FPCWA_0-1735019171159.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Dec 2024 05:46:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-list-all-feature-services-in-portal-and/m-p/1571099#M73424</guid>
      <dc:creator>LindsayRaabe_FPCWA</dc:creator>
      <dc:date>2024-12-24T05:46:32Z</dc:date>
    </item>
  </channel>
</rss>

