ListBrokenDataSources (arcpy.mapping)

Summary

Returns a Python list of Layer objects within a map document (.mxd) or layer (.lyr) file that have broken connections to their original source data.

Discussion

ListBrokenDataSources always returns a Python list object even if only one broken layer is returned. In order to return a single layer object, an index value must be used on the list (e.g., brkLyr = arcpy.mapping.ListBrokenDataSources(mxd)[0]). For loops on a list provide an easy mechanism to iterate through each item in the list (e.g., for brkLyr in arcpy.mapping.ListBrokenDataSources(mxd):).

Some layers within a map document or layer file may be password protected because the user and password information is not saved within the layer file or map document. Map documents that contain these layers typically prompt the user to enter the appropriate information while the document is opening. The arcpy.mapping scripting environment will, by default, supress these dialogs during execution but that means that the layers will be treated as though they have broken data sources. In otherwords, secured layers will not be rendered in any output. If it is necessary for these layers to render appropriately then there are a couple of options. First, save the username and password information with the layers. Second, the CreateArcSDEConnectionFile geoprocessing function allows you to create a connection that is persisted in memory. If this command is used prior to opening a map document (.mxd) with the MapDocument function or a layer file with the Layer function, then SDE layers will render and not appear as broken. Currently, there is not an alternative for secured web services. See the Layer help for a code example.

To learn more about automating the repair of broken layers, refer to: Updating and Fixing Data Sources.

Syntax

ListBrokenDataSources (map_document_or_layer)
ParameterExplanationData Type
map_document_or_layer

A variable that references a MapDocument or Layer object.

Object
Return Value
Data TypeExplanation
Layer

A Python list of Layer objects.

Code Sample

ListBrokenDataSources example:

This script will search for broken data sources in all map documents that exist in a single folder. A report with map document names and broken sources will be printed.

import arcpy, os
path = r"C:\Project"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            print "MXD: " + fileName
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                print "\t" + brknItem.name
del mxd
3/3/2014