layer.attachments.download doesn't work for sublayers

309
6
04-08-2024 04:18 AM
PolzinFiona
Esri Contributor

Hi everyone,

I have an ArcGIS Portal (11.1) referenced Feature Layer that has several sublayers. One of the layers contains photo attachments.

I've had success whenever attachments are not in a sublayer using layer.attachments.download. 

However, I'm stuck trying to figure out how to download the photos given in a sublayer.

Do you know whether there is an option to access sublayers and download their attachments?

Thank you! 

0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor

Hi @PolzinFiona ,

Can you post an example of the code your using?

0 Kudos
PolzinFiona
Esri Contributor

Hi @JakeSkinner,

thank you for your quick reply. This is the code I'm using:

import os
import shutil
from arcgis.gis import GIS
PortalUrl = ""
username = ""
password = ""
FeatureLayerID = ""
SavePath= r""

if username == "" and password == "":
    gis = GIS()
else:
    gis = GIS(PortalUrl, username, password)
FeatureLayer = gis.content.get(FeatureLayerID)
layer = FeatureLayer.layers[0]
print(layer)
layer.attachments.download(save_path=SavePath)

################# Copy image #######################
for root, dirs, files in os.walk(SavePath):
    print(root, dirs, files)
    for f in files:
        if f.endswith('*'):
            print("Copy jpg: {0}".format(str(f)))
            pathOriginal = os.path.join(root,f)
            pathTarget = os.path.join(SavePath,f)
            shutil.copyfile(pathOriginal, pathTarget)

 

Thank you!

0 Kudos
JakeSkinner
Esri Esteemed Contributor

You should just have to change line 15 and specify the sublayer's index number.  For example, if it's the 3rd layer in the service it would be an index of 2:

layer = FeatureLayer.layers[2]

 

PolzinFiona
Esri Contributor

Thank you very much, @JakeSkinner
This is going in the right direction, however, upon further reflection, we realized that our initial inquiry was not quite aligned. Instead of accessing sublayers and their attachments, we need guidance on accessing related tables and their attachments within our Python code. Please excuse the confusion.

We've tried many queries but we seem to be stuck again. Do you know whether it's even possible to access  related tables or attachments in related tables? 

Thank you

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Are you accessing a Feature Service?  I'm able to query the attachments with a Feature Service, but not a Map Service.  Ex:

itemID = 'c820edb1d16c42b5a95fab40634417e8' # Feature Service
lyr = gis.content.get(itemID) 
lyr.tables[0].attachments.get_list() # relationship is with a table
0 Kudos
PolzinFiona
Esri Contributor

 Hi, thank you very much for your reply!

I was not in the office so I could not get back to you earlier. Please excuse the delayed answer!

Yes, we are accessing a feature service!

We tested your code and it works just fine on a simple point feature service with one related table. The feature service is branch versioned.

However, we applied the code on a feature service which has at least 15 related tables which each of may include an attachment. In that case, the code fails, unfortunately.

Just in case, I am providing a screenshot of the table properties:

PolzinFiona_0-1713796660633.png

 

....and a screenshot of the settings applied before publishing the feature service:

PolzinFiona_1-1713796695474.png


Do you know whether there is a limitation regarding the number of related tables? Is it possible to use a code which looks up every single table related to one feature service to verify whether it has an attachment ? If it is possible, how is it possible to access it?

Thank you!

0 Kudos