Select to view content in your preferred language

findAndReplaceWorkspacePath removes layers from TOC

2210
2
01-17-2011 10:27 AM
LoriEmerson_McCormack
Regular Contributor
The following script worked the first few times I used it, but now each of the layers whose workspace paths are fixed, are removed from the Table of Contents.

Code:

import arcpy, string


# access the current ArcMap mxd
mxd = arcpy.mapping.MapDocument("CURRENT")


brknlist = arcpy.mapping.ListBrokenDataSources(mxd)
for brknitem in brknlist:
    arcpy.AddMessage("Found Broken Link for Layer Name: " + brknitem.name)
    if brknitem.supports("WORKSPACEPATH"):
        oldsource = brknitem.workspacePath
        newsource = oldsource.replace("DOCS\GIS","GIS")
        newsource = newsource.replace("docs\gis","GIS")
        brknitem.findAndReplaceWorkspacePath(oldsource,newsource,False)
        arcpy.AddMessage(oldsource + " replaced by newsource: \n" + newsource + "\n")


#refresh
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

del mxd
Tags (2)
0 Kudos
2 Replies
LoriEmerson_McCormack
Regular Contributor
The problem of the layers disappearing from the Table of Contents after running the python script to replace workspace paths appears to be caused by the following:

The script no long works after it is run on an mxd where there is an Annotation file geodatabase.

arcpy.mapping.ListBrokenDataSources(mxd) does not list broken data sources where the feature type is Annotation in a file geodatabase.

I can successfully run the script in the Python window (line by line) within the mxd, but it doesn't work as a script tool from my toolbox even if I try to fix a data source that is a regular feature class.  That is, the script worked fine from the toolbox before I ran it to repair broken links for Annotation in a file geodatabase.  However, after I run the script against Annotation, the script removes layers from the TOC even if I am just trying to repair broken links to polygon/line feature classes.
0 Kudos
RuthEmerick
Emerging Contributor
If you choose to tackle this from python, the link below might be of help. It goes about this by using replaceDataSource instead of using findAndReplaceWorkspacePath, but the gist is the same. I personally chose to use replaceDataSource because it is more flexible for switching SDE's. I've found that my script would break when I tried to fix a data source that did not support datasetName and dataSource; it also broke when there were multiple copies of the same layer in the the TOC, and when there were multiple data frames. You can easily combat the last two by fixing layers one at a time (in a loop) while looking at one data frame at a time (also in a loop). I don't have a solution for annotation classes, but you seemed more interested in polygons and lines.

http://forums.arcgis.com/threads/8573-ReplaceDataSource-not-working-in-script?p=75507#post75507
0 Kudos