Esri / ArcREST

python package for REST API (AGS, AGOL, webmap JSON, etc..)
Apache License 2.0
192 stars 155 forks source link

Ability to 'Replace' Item and keep Feature Service ID. #296

Open delfavero opened 7 years ago

delfavero commented 7 years ago

ArcRest 3.5.3 Enhancement

I am working on a script to custom update a Survey123 xml form with updated choices lists from a SQL database.

After modifying the form based on the database, I cannot see a way to simply replace the xml file in AGOL and keep the same FSID.

The attached script adds a new form with the same name to the folder with a new FSID. I am attempting to use AddItem. Should I use UpdateItem?

syncForms3_ReplacePortalXML_py.txt

from arcpy import AddMessage import os, arcrest

from syncForms1_listFormFSIDs import listFSIDs

formsFolder = r'C:\Users\sdelfavero\ArcGIS\My Survey Designs'

portalURL = 'https://nps.maps.arcgis.com'

fsIDs = listFSIDs(formsFolder)[0]

fsIDs = {'Bear Patrols' : 'e0dd349f25134112af9677c7db38a683'} creds = {'user' : 'xxx', 'pw' : 'xxx'}

def msg(txt): print (txt) AddMessage(txt)

def listXMLs():

XMLs = {}

for pth, dirs, fNames in arcpy.da.Walk(formsFolder):

for fName in fNames:

if fName.endswith('.xml'):

x = os.path.join(pth, fName)

print x

XMLs[fName] = x

return XMLs

def createSecurityHH():

create security dictionary object

msg('\nCreating Security Handler')
user = creds['user']
pw = creds['pw']
sh = arcrest.AGOLTokenSecurityHandler(username=user,
                                      password=pw,
                                      org_url=portalURL)
return sh

def replaceXML(sh, srv, xml, fsID):

portalAdmin = arcrest.manageorg.Administration(securityHandler=sh)
content = portalAdmin.content
item1 = content.getItem(itemId=fsID)
users = content.users
user = users.user()
fldr = 'Survey-' + srv
user.currentFolder = fldr

itemParams = arcrest.manageorg.ItemParameter()
itemParams.title = srv
itemParams.type = "Form"
itemParams.overwrite = True
itemParams.typeKeywords = [u'Form', u'Survey123', u'xForm']
itemParams.thumbnail = xml.replace('.xml', '.png')

item = user.addItem(
    itemParameters=itemParams,
    filePath= xml,
    overwrite=True)
print item.title + " created"

def replaceAllXMLs(): msg('Updating All Forms in ArcGIS Online') sh = createSecurityHH()

for srv in fsIDs:
    fsID = fsIDs[srv]
    xml = os.path.join(formsFolder, fsID, srv + '.xml')
    print '\nReplacing: ' + srv
    print ' ~FSID = ' + fsID
    replaceXML(sh, srv, xml, fsID)

if name == 'main': replaceAllXMLs()

MikeMillerGIS commented 7 years ago

I have not looked into how the survey 123 configuration is stored. Can you break it down into the items that are required? I know there is a feature service, is there another item you are trying to update? The feature service includes domains, you probably would need to change them to update a picklist

tedrick commented 6 years ago

@delfavero - Survey123 Form items are zip files, not XML. Aside form the xForm XML, it may also include CSV files for other choice lists / data lookups, images, audio. (though not necessary, it also includes the xlsx file as well). As it happens, we (the Survey123 team) are working on a script to enable this workflow (we're primarily looking to support CSV replacement, but it should be able to support the XML file as well, so long as it's been properly formatted).