In April 2013, we explained how you could use a Python script to automate the publishing and overwriting of your feature service on ArcGIS.com. This post explains how to perform that same workflow using ArcGIS 10.2 or 10.2.1. The following instructions will get you started, or, scroll to the bottom of this post for extra information on what’s changed at 10.2.
Download the code and set the feature service properties
- Download the Python code (update.py) and settings.ini files in a zip by clicking here. You can check out the the repository on GitHub here.
- Extract the files to a working directory on your machine.
- Download and install the 3rd party module for Python called requests. Requests is used to upload the service definition (.SD) to your ArcGIS.com space. The request module can be downloaded as a zip file here. (Or you can see the full repository on GitHub here.) Inside the zip you’ll find a folder called requests under the master folder. Save the request folder and its contents to the same folder you previously extracted the .py and .ini to. Alternatively you can deploy requests to your local Python installation. Typically ArcGIS installs Python to C:\Python27\ArcGIS10.2\Lib\site-packages.
- Modify the settings.ini with the values specific to your service you’re updating. Make sure to keep the (left side) keywords in capital letters, and do not put quotes around the (right side) properties. The below graphic demonstrates how to set properties. Note, the code searches ArcGIS.com based on the username and service name. If the properties inside the settings.ini file do not match what is on ArcGIS.com, the script will fail to update your service.
- Run the update.py script. For example at command prompt you would run it like this:
This help link explains how to automatically run a Python script as a scheduled task in Windows. You could set your task to run every night at midnight to ensure ArcGIS.com always has your most up to date features.
The authentication mechanism of ArcGIS 10.2 was upgraded to the industry standard of OAuth2. The SignInToPortal tool is no longer used to authenticate with ArcGIS.com (this was the method used in the previous blog post). This new workflow authenticates directly with the ArcGIS.com REST API. The SD file is uploaded and REST calls are made to ArcGIS.com to delete the existing service, and publish the uploaded SD file.
An issue was also identified in 10.2 where some people had problems overwriting their feature service due to bug NIM095154. This issue has been resolved with the release of 10.2.1.
Note, using this overwrite-publish method, the feature service will be assigned a new item ID. The existing service is deleted and this new one with the same name is published. Any client that uses the full URL to the service will continue to work. A client which consumed a feature service via the item ID will need to be updated. WebMaps may have to be updated depending how the feature service was added to the map (full URL or item ID).
Improve the workflow
If you have a code suggestion, just fork it on GitHub, and make a pull request. We’ll review and patch as necessary.