Solved! Go to Solution.
if targetLyr is not None: print "your selected layer is {0}.format(targetLyr.longName)
print "your selected layer is" [0].format(targetLyr.longName)
mxdLocation = arcpy.GetParameterAsText(0) layer = arcpy.GetParameterAsText(1) serverConnection = arcpy.GetParameterAsText(2) newSource = arcpy.GetParameterAsText(3) newMXDname = arcpy.GetParameterAsText(4) def gdbType(connPath): connType = os.path.splitext(connPath)[1] if connType == '.sde': arcpy.AddMessage('\nsde source.') wsType = 'SDE_WORKSPACE' elif connType == '.gdb': arcpy.AddMessage('\nfile gdb source:') wsType = 'FILEGDB_WORKSPACE' else: arcpy.AddMessage('\nunhandled source type') wsType = '' return wsType arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): wsType = gdbType(serverConnection) arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection)) MXDdir = os.path.dirname(mxdLocation) newMXDname = os.path.join(MXDdir, newMXDname + '.mxd') mxd = arcpy.mapping.MapDocument(mxdLocation) lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # single-layer datasource replacement, 1st one listed from TOC. if lyr: if lyr.supports("DATASOURCE") and wsType != '': arcpy.AddMessage('DATASOURCE property is supported for this layer.\n') lyr.replaceDataSource(serverConnection, wsType, newSource) if not arcpy.Exists(newMXDname): arcpy.AddMessage('Please check the {0} layer properties for the source change in your new map doc:'.format(lyr.name)) arcpy.AddMessage('{0}.\n'.format(newMXDname)) mxd.saveACopy(newMXDname) else: arcpy.AddWarning('mxd name is not unique and will not be saved...\n') else: arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n') arcpy.AddMessage('The End.')
# Script Name: Change_datasource # Description: This script allows you to select a specific layer # in your table of content and change the data source. # Created By: Pierre # Date: 29/04/2014 import arcpy import os # Read the parameter values: # 1: Input workspace # 2: Input layer # 3: Set new server connection # 4: Set new source dataset # mxdLocation = arcpy.GetParameterAsText(0) layer = arcpy.GetParameterAsText(1) serverConnection = arcpy.GetParameterAsText(2) newSource = arcpy.GetParameterAsText(3) newMXDname = arcpy.GetParameterAsText(4) def gdbType(connPath): connType = os.path.splitext(connPath)[1] if connType == '.sde': arcpy.AddMessage('\nsde source.') wsType = 'SDE_WORKSPACE' elif connType == '.gdb': arcpy.AddMessage('\nfile gdb source:') wsType = 'FILEGDB_WORKSPACE' else: arcpy.AddMessage('\nunhandled source type') wsType = '' return wsType arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): wsType = gdbType(serverConnection) arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection)) MXDdir = os.path.dirname(mxdLocation) newMXDname = os.path.join(MXDdir, newMXDname + '.mxd') mxd = arcpy.mapping.MapDocument(mxdLocation) # single-layer datasource replacement, 1st one listed from TOC. #lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # multiple-layer datasource replacement... for lyr in arcpy.mapping.ListLayers(mxd, layer): if lyr.supports("DATASOURCE") and wsType != '': arcpy.AddMessage('DATASOURCE property is supported for {0}.\n'.format(lyr.longName)) lyr.replaceDataSource(serverConnection, wsType, newSource) else: arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n') if not os.path.exists(newMXDname): arcpy.AddMessage('Please check the layer properties for ALL layers named \'{0}\', checking the source change in your new map doc:'.format(layer)) arcpy.AddMessage('{0}.\n'.format(newMXDname)) mxd.saveACopy(newMXDname) else: arcpy.AddWarning('mxd name is not unique and will not be saved...\n') arcpy.AddMessage('The End.')