Query Services in ArcGIS Server Manager, then print the Original mxd document path and map service rest url

2309
15
Jump to solution
12-04-2018 01:12 PM
AndresCastillo
MVP Regular Contributor

I would like to find out the Original mxd document path (under the 'General' tab) and map service rest url (under the 'Capabilities' tab) from all services available in ArcGIS Server Manager, rather than using the UI webpage to drill down into each service to find out this info.

This would most likely involve using the ArcGIS Rest API for Server Administration.

So far, I have found the server manifest json file for a specific service using this documentation:

http://gis.esri.com/arcgis/admin/www/doc/index.html#/Service_Manifest/02w000000058000000/

This only lists the mxd path, but not the ms rest url.

In addition, this still only lists that information for one service only.

Ideally, a scripting language would be used to read the information for every service, and print it out to a document, such as a text file.

Any ideas?

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Andres,

Try the following:

import urllib, urllib2, json

# Variables
username = 'siteadmin'
password = 'siteadmin'
server = "gis.esri.com"

tokenURL = 'http://{}:6080/arcgis/tokens/'.format(server)
params = {'f': 'pjson', 'username': username, 'password': password, 'client': 'requestip'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']


baseUrl = "http://{}:6080/arcgis/admin/services".format(server)

catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?token=" + token + "&f=json"))
print("ROOT")
services = catalog['services']
for service in services:
    if service['type']!= 'StreamServer':
        print("\t" + str(service['serviceName']))
        manifestUrl = baseUrl + "/" + service['serviceName'] + '.' + service['type'] + "/iteminfo/manifest/manifest.json" + "?token=" + token + "&f=json"
        dataSource = json.load(urllib2.urlopen(manifestUrl))
        print("\t\t" + dataSource['resources'][0]['onPremisePath'])

folders = catalog['folders']
for folderName in folders:
    if str(folderName) not in ('System', 'Utilities', 'DataStoreCatalogs'):
        print(str(folderName))
        catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "/" + "?token=" + token + "&f=json"))
        services = catalog['services']
        for service in services:
            print("\t" + str(service['serviceName']))
            manifestUrl = baseUrl + "/" + str(folderName) + '/' + service['serviceName'] + '.' + service['type'] + "/iteminfo/manifest/manifest.json" + "?token=" + token + "&f=json"
            dataSource = json.load(urllib2.urlopen(manifestUrl))
            print("\t\t" + dataSource['resources'][0]['onPremisePath'])

View solution in original post

15 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Andres,

Try the following:

import urllib, urllib2, json

# Variables
username = 'siteadmin'
password = 'siteadmin'
server = "gis.esri.com"

tokenURL = 'http://{}:6080/arcgis/tokens/'.format(server)
params = {'f': 'pjson', 'username': username, 'password': password, 'client': 'requestip'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']


baseUrl = "http://{}:6080/arcgis/admin/services".format(server)

catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?token=" + token + "&f=json"))
print("ROOT")
services = catalog['services']
for service in services:
    if service['type']!= 'StreamServer':
        print("\t" + str(service['serviceName']))
        manifestUrl = baseUrl + "/" + service['serviceName'] + '.' + service['type'] + "/iteminfo/manifest/manifest.json" + "?token=" + token + "&f=json"
        dataSource = json.load(urllib2.urlopen(manifestUrl))
        print("\t\t" + dataSource['resources'][0]['onPremisePath'])

folders = catalog['folders']
for folderName in folders:
    if str(folderName) not in ('System', 'Utilities', 'DataStoreCatalogs'):
        print(str(folderName))
        catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "/" + "?token=" + token + "&f=json"))
        services = catalog['services']
        for service in services:
            print("\t" + str(service['serviceName']))
            manifestUrl = baseUrl + "/" + str(folderName) + '/' + service['serviceName'] + '.' + service['type'] + "/iteminfo/manifest/manifest.json" + "?token=" + token + "&f=json"
            dataSource = json.load(urllib2.urlopen(manifestUrl))
            print("\t\t" + dataSource['resources'][0]['onPremisePath'])
AndresCastillo
MVP Regular Contributor

Thank you for your reply Jake,

What API are you using, so I can read documentation regarding this?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

This is using the ArcGIS Rest API for Server Administration API.

0 Kudos
AndresCastillo
MVP Regular Contributor

I get an error in syntax at line 22.

Can you give me a quick run-down of what this python script does?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

That script won't give you the source of the services.  What is the error you are receiving?

0 Kudos
AndresCastillo
MVP Regular Contributor

When I edit and then run with IDLE, it says:

"There's an error in your program: invalid syntax"

When I edit and then run with IDLE (ArcGIS Pro), it says:

"invalid character in identifier"

0 Kudos
AndresCastillo
MVP Regular Contributor

I fixed it.

Since I copied and pasted your script to a text file, then saved as "".py, the IDE was having an issue reading the spacing.

So, I fixed the spacing.

Now, I run it, and get this error:

Traceback (most recent call last):
  File "C:\Users\acastillo\Desktop\server.py", line 11, in <module>
    response = urllib2.urlopen(req)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 431, in open
    response = self._open(req, data)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 449, in _open
    '_open', req)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\ArcGISx6410.4\lib\urllib2.py", line 1197, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Looks like your ArcGIS Server instance is refusing the connection.  You may have HTTP disabled, which means you'll need to update HTTP to HTTPS and change the port from 6080 to 6443 in the baseUrl and tokenUrl parameters.

0 Kudos
AndresCastillo
MVP Regular Contributor

What I just did was change the 'server' variable to the IP address of the server, rather than the domain name, and ran it.

This produced a result, in which service folders and services listed under each folder, and mxd paths are listed.

Then, the last part of the result says:

Traceback (most recent call last):
  File "C:\Users\acastillo\Desktop\server.py", line 38, in <module>
    print("\t\t" + dataSource['resources'][0]['onPremisePath'])
KeyError: 'resources'

0 Kudos