Recently I had a task where, for >10 ‘clients’, I had to:
- Create a unique database user
- Grant privileges to certain datasets in a SDE database
- Save an mxd showing only what was relevant to that client
- Publish a feature service for consumption in ArcGIS Online
While I will leave the specifics of that delightful script for another post, during debugging I came up against an error where the script would fail if the database connection had already been registered with ArcGIS Server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# @date 23/03/2015 | |
# @author Cindy Williams | |
# | |
# Prints the list of databases registered with the | |
# given ArcGIS Server, along with the connection | |
# properties (excluding ENCRYPTED_PASSWORD). | |
# | |
# For use in the Python window in ArcCatalog. | |
# | |
import arcpy | |
import os | |
folder_user = os.environ['USERPROFILE'] | |
folder_arccatalog = "AppData\Roaming\ESRI\Desktop10.3\ArcCatalog" | |
ags_name = "arcgis on dev01 (admin).ags" | |
ags = os.path.join(folder_user, folder_arccatalog, ags_name) | |
for dsi in arcpy.ListDataStoreItems(ags, "DATABASE"): | |
print dsi[0] + "\n\t" + "\n\t".join(dsi[1].split(";")[1:]) | |
In lines 15 and 16, I get the location of the ArcCatalog connections folder for the current user. This is dependent on knowing which version of Arc is installed. I have found a slightly better way (written quite verbosely for clarity):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
import os | |
# Detailed description | |
os_appdata = os.environ['APPDATA'] # Current user's APPDATA folder | |
folder_esri = "ESRI" # ESRI folder name | |
arc_prod = arcpy.GetInstallInfo()['ProductName'] # Get the installed product's name e.g. Desktop | |
arc_ver = arcpy.GetInstallInfo()['Version'] # Get the installed product's version number | |
arc_cat = "ArcCatalog" # ArcCatalog folder name | |
print(os.path.join(os_appdata, | |
folder_esri, | |
arc_prod + arc_ver, | |
arc_cat) |