I am facing a situation in wich our GIS data provider changed the path to their SDE Data server, and also changed the names of the the layers in their SDE Data.

Need to do changes on thousands of .MXDs:

  1. the path to the new .sde connecting file
  2. the layer names of SDE Data

I am not a programmer, but only a GIS mapper:). It is obvious that a script is needed to be batch-applied on all the thousands .mxds, which now have broken links to all the previous layers on SDE Data of our GIS data provider.

Do you have a code for a script, which can successfully change on all the maps - the path to the old .sde connecting file into a new path to the new .sde connecting file, & also to replace the old layers with the new layers which have different names now?


I ran this recently on many maps (not thousands though!):

import arcpy
import os
import glob

def fix_data_sources(mapDoc):
    sde = r'\\arcserver2\SDE_Connections'
    bad = []
    mxd = arcpy.mapping.MapDocument(mapDoc)
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports('SERVICEPROPERTIES'):
            if lyr.serviceProperties['Server'] == u'arcserver1_sqlexpress':
                find = lyr.workspacePath
                _name = os.path.basename(find)
                if not _name.endswith('.sde'):
                    _name += '.sde'
                replace = os.path.join(sde, _name)
                    lyr.replaceDataSource(replace, 'SDE_WORKSPACE', lyr.datasetName)
                        lyr.findAndReplaceWorkspacePath(find, replace)
                        worked = True
                        worked = False
                    if not worked:    
                        bad.append((mapDoc, lyr.name))
            if lyr.supports('DATASOURCE'):
                bad.append('Not an SDE Layer: "{0}"\n\tdata path: {1}'.format(lyr.name, lyr.dataSource))

        bad.append('\n\n{0} update failed\n\n'.format(mapDoc))
    del mxd
    print 'Fixed data sources for: {0}'.format(mapDoc)

    # make text file if data was not repaired
    if [x for x in bad if x != '\n']:
        txt = os.path.join(os.path.dirname(mapDoc), '{0}_paths_to_fix.txt'.format(os.path.basename(mapDoc).split('.')[0]))
        fl = open(txt, 'w')
        for item in bad:
            if isinstance(item, tuple):
                mapD, lyr = item
                fl.write('{0} update failed for layer: {1}\n'.format(mapD, lyr))
                fl.write('Could not update: "{0}"\n'.format(item))

if __name__ == '__main__':
    for mapDoc in glob.glob(r'\\arcserver2\WebGIS\*\Web_Maps\*.mxd'):

It successfully fixed around 90% of them, but not all for whatever reason. This was for an SDE upgrade from SQL Express to Full Blown SQL Server. You would need to change the variables to fit your needs.

It creates a text file in any folder that contains links that couldn't be fixed and lists those layers so you know what you may need to manually fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.