We have an ArcGIS map service that includes about 50 layers, and many of them have related tables. I need to build a schematic of this service that includes layer name, source feature class (or table), attributes that are included -- but I also need to find which table(s) are related to a given layer, which is where I am stuck.

I have access to the source MXD. So in theory, I can open each layer in ArcMap and look at it. But, with 50 layers, I'm looking for a Python method -- especially one that I can reuse if this MXD/service changes in upcoming months (layers or tables added/removed, e.g.) to re-create my list of referenced data.

I can use the arcpy.mapping module to list the layers/tables, but I do not know how to use it to ask about relationships to other layers/tables.

  • 2
    One thing you need to clarify; are the relationships in_memory and stored as the layer in the MXD (i.e you created a relationship in ArcMap and saved the mxd) or are they relationships stored in a geodatabase?
    – Hornbydd
    Commented Jan 14, 2019 at 17:07
  • They're apparently relationship classes in the SDE geodatabase. (I only got this MXD this morning, so had to look!)
    – Erica
    Commented Jan 14, 2019 at 17:36
  • Have a look here?
    – Hornbydd
    Commented Jan 14, 2019 at 20:49
  • I think you should focus this question on ArcPy and if you still need to ask about the ArcGIS REST API then that could be done in a separate question.
    – PolyGeo
    Commented Jan 14, 2019 at 22:54
  • @Hornbydd I think a relationship class property will only work if I know there is a relationship class involved. I suppose I can get all the relationship classes in the SDE and then use a list of them to compare against the list of the feature classes that participate in the map, but that's a relatively awkward workaround unless there's nothing more direct :)
    – Erica
    Commented Jan 15, 2019 at 0:02

1 Answer 1


Here is a Python 3 script that crawls a map service and prints out all of the relationships. You should just need to change the map service url.

import urllib.request
import json

MapServiceURL = 'MapServiceUrl'

relationships = []

# Get the map service definition to get a list of the layers.
with urllib.request.urlopen('{}?f=json'.format(MapServiceURL)) as url:
    ServiceInfo = json.loads(url.read().decode())
    layers = ServiceInfo['layers']

# Function that requests the relationship property for a layer in a service.
def fetchLayerRelationships(layerId, name):
    with urllib.request.urlopen('{}/{}?f=json'.format(MapServiceURL, layerId)) as url:
        LayerInfo = json.loads(url.read().decode())
        if LayerInfo.get('relationships'):
            for relate in LayerInfo.get('relationships'):
                relationships.append((name, relate.get('name')))

# Iterate over the services layers and fetch the relationships.
for layer in layers:
    fetchLayerRelationships(layer.get('id'), layer.get('name'))

for rel in relationships:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.