<?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 Loop to all items of a Feature Layer using ArcPy in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/loop-to-all-items-of-a-feature-layer-using-arcpy/m-p/1136526#M7035</link>
    <description>&lt;P&gt;&lt;FONT size="3"&gt;I have found some examples of how to iterate through the contents of a Feature Class. However, this does not work for a Feature Layer:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT face="courier new,courier"&gt;import arcpy&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;import arcgis&lt;BR /&gt;from arcgis.gis import GIS&lt;BR /&gt;gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;search_result = gis.content.search("ArcGIS_content", "Feature Class")&lt;BR /&gt;item = search_result[0]&lt;BR /&gt;FeatureLayer = item.layers[0]&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;with arcpy.da.SearchCursor(FeatureLayer, ["OID@"]) as cursor:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;for row in cursor:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; attachments_list = FeatureLayer.attachments.get_list(oid = row[0])&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(attachments_list) &amp;gt;= 1:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("Found", str(len(attachments_list)), "attachments")&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Is there an efficient way to do what I tried here with a FeatureLayer; namely, to iterate through all of its features (in my case point features) and extract information (the idea is to download the attachments and save coordinates and some other attribute values)?&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2022 18:56:10 GMT</pubDate>
    <dc:creator>Manu</dc:creator>
    <dc:date>2022-01-24T18:56:10Z</dc:date>
    <item>
      <title>Loop to all items of a Feature Layer using ArcPy</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/loop-to-all-items-of-a-feature-layer-using-arcpy/m-p/1136526#M7035</link>
      <description>&lt;P&gt;&lt;FONT size="3"&gt;I have found some examples of how to iterate through the contents of a Feature Class. However, this does not work for a Feature Layer:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT face="courier new,courier"&gt;import arcpy&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;import arcgis&lt;BR /&gt;from arcgis.gis import GIS&lt;BR /&gt;gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;search_result = gis.content.search("ArcGIS_content", "Feature Class")&lt;BR /&gt;item = search_result[0]&lt;BR /&gt;FeatureLayer = item.layers[0]&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;with arcpy.da.SearchCursor(FeatureLayer, ["OID@"]) as cursor:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;for row in cursor:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; attachments_list = FeatureLayer.attachments.get_list(oid = row[0])&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(attachments_list) &amp;gt;= 1:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("Found", str(len(attachments_list)), "attachments")&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Is there an efficient way to do what I tried here with a FeatureLayer; namely, to iterate through all of its features (in my case point features) and extract information (the idea is to download the attachments and save coordinates and some other attribute values)?&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 18:56:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/loop-to-all-items-of-a-feature-layer-using-arcpy/m-p/1136526#M7035</guid>
      <dc:creator>Manu</dc:creator>
      <dc:date>2022-01-24T18:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Loop to all items of a Feature Layer using ArcPy</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/loop-to-all-items-of-a-feature-layer-using-arcpy/m-p/1136622#M7037</link>
      <description>&lt;P&gt;You won't need a cursor for that. See feature layer documentation to query for objectIds and then list atttachments &lt;A href="https://developers.arcgis.com/python/guide/working-with-feature-layers-and-features/" target="_self"&gt;here&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/python/guide/working-with-feature-layers-and-features/" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Continuing from your script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import arcgis
from arcgis.gis import GIS
gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)

search_result = gis.content.search("ArcGIS_content", "Feature Class")
item = search_result[0]
fl = item.layers[0]
fs = fl.query(where="1=1", outFields="field1,field2")
oidFieldName = "objectid" # replace when necessary
oids = [f.attributes[oidFieldName] for f in fs.features]
attachments = [fl.attachments.get_list(oid=oid)} for oid in oids]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;attachments is a list of lists, where the nested list is shaped like this (for each oid):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[{'contentType': 'image/png',
  'id': 1,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 2,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 4,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394}]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can download these using:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
local_path = "SOME_FOLDER"
for attachment_list in attachments:
    for attachment in attachment_list:
        fl.attachments.download(save_path=os.path.join(local_path, attachment['name']), oid=attachment['parentObjectId', attachment_id=attachment['id'])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 21:45:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/loop-to-all-items-of-a-feature-layer-using-arcpy/m-p/1136622#M7037</guid>
      <dc:creator>HuubZwart</dc:creator>
      <dc:date>2022-01-24T21:45:59Z</dc:date>
    </item>
  </channel>
</rss>

