Determine Broken SDE Connections

1067
10
02-03-2012 05:02 AM
MichaelVolz
Esteemed Contributor
To All Python Users:

I am using the ListBrokenDataSources method in python to get all the broken datasources in an mxd.  Now I want the ability to determine the type of connections, specifically SDE, that the broken links are.  I tried to loop through the list of broken data source layers and use the serviceproperties method to determine if it was SDE, but that is returning an error.  Does anyone know if this can be done?  If so, what python method would I use to go about getting this information.  Thanks.
Tags (2)
0 Kudos
10 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Michael,

The 'serviceProperties' property will not work for broken data sources.  However, the following should work to see which layers were stored in SDE:

mxd = arcpy.mapping.MapDocument("C:\TEMP\python\Airports.mxd")
broken = arcpy.mapping.ListBrokenDataSources(mxd)
for n in broken:
    for lyr in arcpy.mapping.ListLayers(mxd, n):
        if '.sde' in lyr.dataSource:
            print lyr.name

del mxd  
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

The following code did not work for me to identify an SDE feature class because '.sde' did not appear in the dataSource property that I printed out to the progress window.  Are you sure '.sde' is supposed to be in the dataSource property for an SDE feature class?  All that showed up in the dataSource property for an SDE feature class was \'feature class dataset name.feature class name'.

mxd = arcpy.mapping.MapDocument("C:\TEMP\python\Airports.mxd")
broken = arcpy.mapping.ListBrokenDataSources(mxd)
for n in broken:
    for lyr in arcpy.mapping.ListLayers(mxd, n):
        if '.sde' in lyr.dataSource:
            print lyr.name

del mxd.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
What do you receive when you run the following:

import arcpy
from arcpy import env
env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("C:\TEMP\python\Airports.mxd")
broken = arcpy.mapping.ListBrokenDataSources(mxd)
for n in broken:
    for lyr in arcpy.mapping.ListLayers(mxd, n):
        print lyr.dataSource

del mxd 
 

Note:  You will need to update the path to the MXD.

You should receive something along the lines of:

Database Connections\SDEConnection.sde\vector.VECTOR.ap

All SDE feature classes come from a SDE connection file so '.sde' should be in the path.
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

I modifed my code slightly to duplicate your code and I am still receiving dataSource property for an SDE feature class was \'feature class dataset name.feature class name' (e.g. ADMIN_BOUNDARIES.MUNIS) instead of something like Database Connections\SDEConnection.sde\vector.VECTOR.ap for an SDE broken link.

I was able to get the dataSource property in your format if I called this property from an SDE feature class that was not broken, so I'm guessing that the dataSource property works differently if the SDE connection is broken.

Should I place a call with ESRI Technical Support for further assistance with this issue?  Thanks.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I tested with broken data sources, and it still returned the full path.  Can you post your code?  Be sure to wrap it in the
 tags ("#" symbol).  Also, what service pack are you running for ArcGIS Desktop?
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

As requested, here is my code that returns different results from your code:

import os, arcpy, sys
from arcpy import env
env.overwriteOutput = True

mxd_match = ".mxd"

for root, dirs, files in os.walk(arcpy.GetParameterAsText(0)):

    fListLength = len(files)
        
    if (fListLength != 0):
        n = 0
        for f in files:
            
            if f.endswith(mxd_match):

                full_path = root + "\\" + str(f)

                try:
                    mxd = arcpy.mapping.MapDocument(full_path)
                    
                    broken = arcpy.mapping.ListBrokenDataSources(mxd)
                    for n in broken:
                        for lyr in arcpy.mapping.ListLayers(mxd, n):
                            arcpy.AddMessage("The broken SDE layer dataSource is " + lyr.dataSource)
                    
                    del mxd
                except:
                    print arcpy.GetMessages(2)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Everything appears to be correct, and it worked for my MXDs with broken data sources.  I would suggest logging a call with Tech Support.
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

Do you think it might matter how the SDE connection is broken?  I created a new SDE version owned by myself off the default version.  I added data from SDE with this version to an mxd and saved the mxd.  I then deleted the SDE version in ArcCatalog which breaks the link.  I then ran my python script on this mxd with the broken SDE link.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, that's it!  I was able to reproduce it that way.  Do your MXDs only contain SDE layers, or do they have a mix of data?  You can alter the code to find layers that do not have '.sde' in the dataSource.  Below is an example that will only show broken data sources for SDE layers in an MXD that also contains broken data sources for File Geodatabase feature classes and shapefiles:

import os, arcpy, sys
from arcpy import env
env.overwriteOutput = True

mxd_match = ".mxd"

for root, dirs, files in os.walk(r"C:\temp\python"):

    fListLength = len(files)
        
    if (fListLength != 0):
        n = 0
        for f in files:
            
            if f.endswith(mxd_match):

                full_path = root + "\\" + str(f)

                try:
                    mxd = arcpy.mapping.MapDocument(full_path)
                    
                    broken = arcpy.mapping.ListBrokenDataSources(mxd)
                    for n in broken:
                        for lyr in arcpy.mapping.ListLayers(mxd, n):
                            if '.sde' not in lyr.dataSource:
                                if '.gdb' not in lyr.dataSource:
                                    if '.shp' not in lyr.dataSource:
                                        print "SDE broken data source is " + lyr.name
                    
                    del mxd
                except:
                    print arcpy.GetMessages(2)
0 Kudos