3

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
  • not if the data is sde data? you can manually view data source but the very small hint you gave tells me you are doing this on a large scale. please update your question with more details of your requirements btw . sde information from mxd. what sde information. sde meta-data? – Brad Nesom Oct 1 '13 at 16:28
  • I'd take a look at example 3, here: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… scraping through lyr.serviceProperties is more what your looking for. lyr.dataSource on an SDE layer will return the file path to the .sde, which you won't have. – DPierce Oct 1 '13 at 17:32
  • @DPierce thanks! That's exactly what I needed, not layer.dataSource but layer.serviceProperties. Since this is my first post here, not sure how to mark your comment as answered and give you credit? – govwrkr Oct 1 '13 at 18:49
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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