AnsweredAssumed Answered

How do I run a Python script inside ArcMap to change datasources

Question asked by jsn on Jun 11, 2018
Latest reply on Jun 11, 2018 by Dan_Patterson

Hello everyone,

 

I need to run the python script below that changes layer datasources inside ArcMap when the mxd  is open.

 

How do I run the script with ArcMap Open?

 

 

import arcpy
from arcpy import env
from arcpy import mapping

 

newDataSource = r"Database Connections\NewSDEConnection.sde"
mapdoc = arcpy.mapping.MapDocument("CURRENT")
#mapdoc.findAndReplaceWorkspacePaths("<the directory you are changing from>" , newDataSource)

 

for df in mapping.ListDataFrames(mapdoc, "*"):
                    for lyr in mapping.ListLayers(mapdoc, "*", df):
                     
                        try:
                            if lyr.supports("DATASOURCE"):
                                theData = lyr.dataSource
                                print "the Data " + theData
                                theWorkspacePath = lyr.workspacePath
                                print  theWorkspacePath

 

                                if theData.find(".ImageServer") == -1:
                                    if  ".sde" in theWorkspacePath.lower():
                                        mxd.findAndReplaceWorkspacePaths(theWorkspacePath,newDataSource,False)
                                 
                               
                        except Exception as e:
                            print "The following error has occured:"
                            print e.message

 

                        except AttributeError:
                            pass

 

mapdoc.save()
del mapdoc

Outcomes