Here is the solution I found, I don't know if it's the best (it would be nice to capture symbology as well), but it does work and is straightforward. It's possible to grab a feature service and attachments and relationship class all in go from the server itself in a file-gdb.
Esri has updated the export tools. There are now two official routes that I'm aware of for exporting feature service data with attachments.
As a normal user
Navigate to the feature service's main about page (http://arcgis.com/home/item.html?id=xxx123456xxx
), and use the [Export] link at right side, select FGDB for output format, and you're done. (If the export link is missing, [Edit] and check the Export Data box first.)
Interactively via API
Point browser to http://services.arcgis.com/{xxx123456xxx}/arcgis/rest/services/{folder_name}/FeatureServer//createReplica
Set values to the below, unlisted items can just use default. Click on the resultant “statusURL”, and then “Result Url”, save zip file in wherever and extract a file-gdb with everything intact (except symbology).
Replica Name some_meaningful_name
Layers 0,3 (select by index number)
Return Attachments TRUE
Return Attachments by Url TRUE
Create Replica Asynchronously TRUE
Sync None
Data Format FileGDB
For interest, I also found a script to this the hard way, extracting records and attachments individually and recreating locally; see AGO_PullHostedFeatures.py by Owen Evans.
I suspect that the problem was that there were 2 layers with the same name, but I'm not sure. Anyway, I modified my script so it will ignore all errors, because it is enough for what I'm trying to accomplished. The script that worked for me:
import os
import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\data from AGOL\Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
destPath = r"D:\data from AGOL"
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if lyr.isFeatureLayer == True:
d = lyr.name.encode('ascii', 'ignore')
file_name = d + ".shp"
dest1 = os.path.join(destPath, file_name)
print file_name
print type(file_name)
print dest1
try:
arcpy.CopyFeatures_management(lyr, dest1)
except:
pass
Best Answer
With ArcREST you can export/download feature services (plus plenty of other tasks). You can find the source code on GitHub and a sample blogpost that shows how to do it on an older version, though it should be relatively similar to ArcREST 3.
There are some sample scripts in the repo that should get you started, as well.