2

Using this example I am trying to loop through my Arcgis Online account using ArcREST in order to download feature layers locally. However I have not been able to drill down into each folder to acquire each service name with .content in order to run the .exportItem. I can get the folder names but I can't get access to the feature layers in the folders.

import os
import arcpy as env
import arcrest
import arcpy

arcpy.env.workspace = r"C:\xxx\temp"
uname = "xxx"
pword = "xxx"

site = "http://xxx.xxx.arcgis.com"

sh = arcrest.AGOLTokenSecurityHandler(uname,pword,site)
admin = arcrest.manageorg.administration.Administration(securityHandler=sh)

content = admin.content
user = content.users.user(uname)

for folder in user.folders:
    print folder
    #This line below was added after original post
    for sub_folder in folder:
       print sub_folder

Edit: added a for loop within the original loop to attempt to access the contents of the folders. The variable sub_folder prints only the contents of the root folder

10
  • You are saying that you cannot get access to features in subfolders. What is the response you get from the script?
    – Stefan
    Dec 21, 2015 at 11:50
  • It only prints the items in the root folder it does not loop through the contents of the other folders...i[title] returns all folder names but user.items only returns the roots content. i.items does the same. I need to be able to get to the contents of the folders to export them.
    – risail
    Dec 21, 2015 at 12:23
  • You also need to loop the subfolders in order to get the contents. Try to create a new for-loop in the existing one. Let me know if you need any help with it.
    – Stefan
    Dec 21, 2015 at 12:28
  • access to the sub folders is my question..Your help would be appreciated
    – risail
    Dec 21, 2015 at 13:36
  • Here is a start: for folder in user.folders: for subfolder in folder: print subfolder.items
    – Stefan
    Dec 21, 2015 at 13:54

2 Answers 2

3

I was not able to accomplish this solely with ArcREST. I solved it by creating a list with the urls to the folder. Which gave me the ability to perform actions on the Feature Services located within folders

p = {"f": "pjson", "token": token}
token = sh.token

url = []
for folder in user.folders:
    fld_id = str(folder['id'])
    user = str(folder['username'])
    fldr = root_url + "/users/" + user + "/" + fld_id 
    url.append(fldr)



for each in opp_url:
    #print each
    get = requests.get(each, params=p)
    obj = get.json()
    for fs in obj['items']:
        if fs['type'] == 'Feature Service':
            titelid = fs['title']
            exportedItemId = fs['id']
            print exportedItemId
            #Continue doing some operation on Feature Services once ids are obtained 
2

I finally figured this out...

admin = arcrest.manageorg.administration.Administration(securityHandler=sh)
content = admin.content
user = content.users.user(uname)

# Iterate through all content in each folder
for folder in user.folders:
    fName = folder['title']
    user.currentFolder = fName
    print ('--------------------------------------------')
    print ("In Folder: {}".format(fName))
    for item in user.items:
        print " - {}".format(item.title)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.