Update Webmap (json) with Python

2415
2
03-28-2017 03:11 AM
Nicole_Ueberschär
Esri Regular Contributor

I would like to update a webmap through json with Python. I know that I could use the AGOL Assistant to change the json of a specific webmap but just as an example: If I want to change the same thing in all of my webmaps (e.g. changing a layer or a bookmark name or the bookmarks in general...) it would be more comfortable to "grab" all my webmaps from my AGOL content, make the changes (at the moment I'm just replacing for example "Berlin" as a bookmark name by "Berlin, Germany") and update my webmaps in AGOL. 

I have seen this ArcGIS REST API Update function but I don't understand how to use it. 

I have tried update-webmap-json at master · from GitHub but I don't see at which point I should apply my edits to the json in order to see if anything happened when doing the update. Did anyone manage to work with this?

I learned thanks to this post Edit or update ArcGIS Online Web Map data using the REST API that I can pull the data with http://www.arcgis.com/sharing/rest/content/items/ITEMID/data and should be able to update with http://www.arcgis.com/sharing/rest/content/users/USER_ID/items/ITEM_ID/update. 

I attached my script in case someone feels like testing. 

Here is in brief what I do with my script: 

  • set username and password
  • define (for testing) which webmap to take
  • Read map(s) from Content and save to mxd and json
  • Before updating I open the json, do my changes, save it to the same file.
  • At the next step I am not sure which "stage" of the json I have to use to upload it and I don't really understand what lines from line 51 are actually doing. Maybe someone can point me where this actual update request is better described. 
  • The result is that my script runs smoothly showing all the prints, no errors, but when I check the bookmark in my webmap that I intended to change it is still the same. 

Thank you very much in advance!

0 Kudos
2 Replies
JonathanQuinn
Esri Frequent Contributor

Have you taken a look at the HTTP requests that are made within the browser when going through this manually?  You can capture them using Fiddler or the dev tools within the browser you're using.  You're essentially just recreating those requests in Python.

Specific to your question regarding lines 51 through 65, it starts earlier at line 39.  It seems like you have JSON that's stored in two files.  You read the contents from one file, (filein on line 43), and then replace a string, (line 46), and then save it to fileout, (line 48).  You then read the contents again on line 52, (you can actually just reuse the newdata variable from 48), and then you de-serialize the JSON into text, (line 55), and then construct the "payload" for the update request, (line 56).  Payload data has to be encoded, (line 59), and then you construct a Request object, (line 60), and then retrieve the response from the request, (line 61).

You're not really getting the response of the request, so potentially, there's a problem you're not capturing.  You can just print the response after line 61 to see if the request went through.  I think you'll see {"status":"success"...} or something similar.

The other thing is take a look at the requests in Fiddler or the dev tools, you may be missing a required parameter.  For example, I don't see a token passed along wiht the request, so the response is likely a 498 error, token required.  That may be handled by lines 70 and 71, but I don't think that handler is being used if you're submitting the requests yourself using urllib and urllib2.  You'll need to provide a token.

Nicole_Ueberschär
Esri Regular Contributor

Thank you Jonathan! Looks like I have to dig a bit deeper... 

0 Kudos