In this article you will learn how to re-establish the missing data inside a MXD file by renaming the feature class or shapefile using Python.
Unlike my previous article “PYTHON GIS - How to repair paths of several MXD files massively”, in this article the path where the spatial data is stored has not changed, but the name of the data itself has changed, either because the conception of the data model changed or because a different layer was used than the one that originally should be used by default among other reasons for renaming.
To retrieve the link of the renamed spatial information with the layer stored in an MXD file the normal procedure is as follows:
How to assign a spatial object to a layer?
Option 1:
1) Open the MXD file.
2) Go to the layer with the broken link (a), right click and press the “Properties...” item (b).
3) Go to the “Source” tab (c) and click on “Set Data Source...” (d).
4) Find the spatial object (Shapefile/Feature Class) with the new name that corresponds to the layer (e) and press the “Add” button (f).
5) Click on “Accept” in the “Layer Properties” window (g).
6) Save MXD file.
Option 2:
1) Open the MXD file.
2) Go to the layer with the broken link (a), double click (left) on the layer name (b), which will automatically display the “Layer Properties” window.
3) Go to the “Source” tab (c) and press “Set Data Source...” (d).
4) Find the spatial object (Shapefile/Feature Class) with the new name that corresponds to the layer (e) and press the “Add” button (f).
5) Click on “Accept” in the “Layer Properties” window (g).
6) Save MXD file.
Option 3:
1) Open the MXD file.
2) Go to the layer with the broken link, click or double click (left) on the layer checkbox (a), which will automatically display the “Set Data Source” window (b).
3) Find the spatial object (Shapefile/Feature Class) with the new name that corresponds to the layer (c).
4) Click on “Add” (d).
5) Save MXD file.
This procedure no matter which option is chosen, has to be repeated for each layer within the MXD files that have lost their data source, which can be quite time consuming, if the problem is with several MXD files, so I will show you how it can be much more efficient using Python code (Don't worry you don't need to know how to program to do it).
How to repair MXD (ArcMap) file data sources automatically?
Step 1: Open a blank ArcGIS file.
Step 2: Open the Python window in the Standard toolbar.
Step 3: Backup the original MXD files.
Step 4: Identify the missing paths and the current paths to the data. To check the missing paths, right click on the layer (a), press the “Properties...” button (b) and go to the “Source” tab (c), in the “Location:” item (d) you will find the path where the spatial data was previously stored and the previous name (e).
Step 5: Copy into a note blog the following code and replace the text C:\EJEMPLO\EJEMPLO in line #02 with the path to the folder where you have stored the MXD files that present the problem.
Step 6: In line #09 of the code replace C:\EJEMPLO\EJEMPLO.gdb\Old_feature_class with the old and/or missing path including the name of the shapefile or feature class as identified in step 4.
Step 7: In line #10 of the code replace C:\EJEMPLO\EJEMPLO.gdb with the new path where the spatial information is stored. In the same line #10 replace New_feature_class with the name of the shapefile or the feature class.
Step 8 (Optional): To define if the object to change is in a .mdb database, replace in line #10 FILEGDB_WORKSPACE by ACCESS_WORKSPACE, or if the object is a shapefile replace FILEGDB_WORKSPACE by SHAPEFILE_WORKSPACE.
Step 9: Copy the modified code into the Python window and press the Enter key twice.
After a few seconds you will see how the modification date of the ArcMap files starts to be updated, and when you open it you will notice the recovery of the link in the layers that presented this problem.
Notes:
- The symbology may have been affected since the fields used for the graphical representation may not match the original ones, so I invite you to read my article “PYTHON GIS - Modify symbology of a layer in several ArcGIS files” where you can get an idea of how to fix this quickly.
- In case you also need to change the Definition Query of the layer I invite you to read my article “PYTHON GIS - How to modify and/or create a Definition Query to a layer within several ArcGIS files (ArcMap / MXD)”.
I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.





