How to fix broken link inside mxd with script?

6370
9
05-08-2012 04:00 AM
haifaAl_Yazeedi
New Contributor
Hello

Is there a way to fix broken link inside mxd using a script?

We have old mxd with many layers (200 shapefiles)  and we decided to rename the shapefiles using our naming standard and then move them to ArcSDE BUT the relink job is painful. Is there a way in arcpy.mapping scripting?

I tried some tool from ESRI for fixing broken link but it is still painful. I need to find a script which will do the below:

Replace the old path of the layer (ex: C:\Test.shp) to the new path  (ex: SDE.Blocks) inside the old mxd.

Thanks,
Haifa
Tags (2)
0 Kudos
9 Replies
KorySteele
New Contributor III
I am also eagerly awaiting a response to this.
I often have to move .mxds between my HDD and a shared drive.  This one issue is the only impedance to that.
0 Kudos
TimothyHales
Esri Notable Contributor
You can use arcpy.mapping to change the data source paths within an MXD.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"C:\Project\Data2")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd


Updating and fixing data sources with arcpy.mapping
MichaelVolz
Esteemed Contributor
halyazeedi:

I think you would need to modify the script from thales007 to have the destination path be a connection to SDE that you have saved on your computer with ArcCatalog.  The third line with findAndReplaceWorkspacePaths is a composite of the 1st two lines (that have been commented out) where the source is the current shapefile and the destination is the SDE geodatabase.  You might have some issues with field types in the transition from shapefiles to SDE that you would need to work through.  You would also need to ensure that you have read access to all the associated feature classes in SDE from the SDE connection that you are using.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project_default.mxd")
'mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"C:\Project\Data2")
'mxd.findAndReplaceWorkspacePaths(r"C:\Project\Connection to Default.sde", r"C:\Project\Connection to Version1.sde")
mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"C:\Project\Connection to Version1.sde")

mxd.saveACopy(r"C:\Project\Project_V1.mxd")
del mxd
0 Kudos
haifaAl_Yazeedi
New Contributor
Thank you all for your help.

There is two issue here first one is the path is changed from shape file to sde and the second issue is the shape files name is changed also..when we imported the shape file into sde we renamed it (ex: test.shp renamed to sde.Blocks).

Is there way tyo fix both issue using a script?

Thanks,
Haifa
0 Kudos
MichaelVolz
Esteemed Contributor
Layer.replaceDataSource is the method you are looking for.  I believe this script would be more complex as you will need to map the old datasource to the new datasource.  I have done this by calling a def function where it looks for a specific input layer name and that is mapped to the new layer name with its new parameters.  In your case this def could get pretty big as you say you have 200 shapefiles in your mxd(s) that need to be linked to new SDE layers.

The reference from thales007 has the link to this documentation.
0 Kudos
AntoineCantin
New Contributor III

I know it's an old post but , I was wondering if there is a way to use this script if my shapefiles are in two different documents?

Thanks,

0 Kudos
WesMiller
Regular Contributor III

Yes you would have to run the script on each mxd that has the broken link in it.

RebeccaStrauch__GISP
MVP Emeritus

fwiw, I have an addin (works with 10.2.x/10.3.x) that can look at all the mxd in a folder (and subfolders), provide a report of all broken links and allow you to update a .csv file and then fix most (if not all) of the broken links).  Always do a test on a copy first...but, in case you are interested

Python addin for data inventory and “broken-link” repair.

Not perfect, and may not be work the effort for just two mxd docs...but if yo have a lot, or if machines/paths change, may be worth it.

0 Kudos
AntoineCantin
New Contributor III

I'll give it a try, thanks!

0 Kudos