[GIS] ArcGIS 10.0 get sde information out of .mxd

arcgis-10.0enterprise-geodatabaselayerspython

The overall goal here is to iterate through several directories of .mxd files and document which SQL Server instance of SDE they are pointing to, with the database name and feature class names.

All of our connections are direct connect, no SDE services running.

I'm trying to get the server/database info out of an mxd using ESRI's example here http://support.esri.com/cn/knowledgebase/techarticles/detail/41112, but it keeps trying to look at the .sde file that was used to create the mxd instead of getting the info straight from the .mxd.

specifically, this line

desc = arcpy.Describe(sub_str)

throws an error that it can't find the .sde file. It's not going to since we have many users all with their own .sde connection files. The sub_str ends up being C:\Users\username\etc… depending on who created the .mxd.

Is there another way to get the datasource info out of an .mxd that doesn't rely on the .sde file? (ArcGIS 10.0)

EDIT: SOLUTION USED

On the off chance my horrible python skills can be of use to someone, this is the solution I ended up with. I know it can be cleaned up, but I just needed quick and dirty feel free to critique:

def mxdInfo(inMxd, inCsv):

mxd = arcpy.mapping.MapDocument(inMxd)

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("SERVICEPROPERTIES"):
        servProp = lyr.serviceProperties
        lName = lyr.name
        dbName = servProp.get('Database', 'N/A')
        serviceName = servProp.get('Service', 'N/A')
        serverName = serviceName.split(':')
        userName = servProp.get('UserName', 'N/A')
    else:
        print "SERVICEPROPERTIES not supported on " + lyr.name
        continue
    if lyr.supports("DATASOURCE"):
        dSource = str(lyr.dataSource)
        fcName = dSource.split('.sde\\')
        print "Layer Name: " + lyr.name
        print "Feature Class: " + fcName[1]
    else:
        print "DATASOURCE not supported on " + lyr.name
        continue
    #write out to csv
    if not os.path.isfile(inCsv):
        csvFile = open(inCsv, 'wb')
        try:
            writer = csv.writer(csvFile)
            writer.writerow(('MXD', 'Layer Name', 'Server', 'Database', 'Feature Class', 'User Name'))
            writer.writerow((inMxd, lName, serverName[2], dbName, fcName[1], userName))
        except:
            print "error writing first row of csv"
    else:
        csvFile = open(inCsv, 'ab')
        try:
            writer = csv.writer(csvFile)
            writer.writerow((inMxd, lName, serverName[2], dbName, fcName[1], userName))
        except Exception as e:
            print "error writing to csv"
            print e
del mxd

Best Answer

I'd take a look at example 3, here:

Scraping through lyr.serviceProperties what you're looking for. lyr.dataSource on an SDE layer will return the file path to the .sde connection file, which you won't have access to. You should be able to loop through the serviceProperties and get what you need.

From the linked ArcGIS Help:

for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("SERVICEPROPERTIES"):
    servProp = lyr.serviceProperties

lyr.serviceProperties being a read-only python dictionary.

Related Question