AnsweredAssumed Answered

Replace data source of SDE layers in map documents (mxd)  with Python’s replaceDataSource method

Question asked by kkumbadaesri on Jul 19, 2016
Latest reply on Jul 20, 2016 by kkumbadaesri

Hello all,

 

I need some help to replace the data source of old SDE layers
in the map documents. The code outlined below is not working properly resulting
result in a broken link.

 

We recently reorganized our ArcSDE database resulting in all
previous feature classes reorganized under the feature dataset with a new name.
In the interim, the SDE has both old and new data.  That results in a lot of map documents, in
which the data sources of all the layers need to be replaced with new workspace
path. Eventually, we want to delete the old sde feature classes directly under
the ArcSDE.

 

Based on our research, lyr.replaceDataSource seems a
plausible solution since it provides an option of replacing both work space
path and dataset name.

 

I greatly appreciate any input.

 

Thank you,

 

--------------------------------------------------------------------------------------------------

 

#loop through the folder to locate mxds and layers within it and replace data source
of SDE layer

for fileName in os.listdir(path):

     fullPath = os.path.join(path,
fileName)

    
if os.path.isfile(fullPath):

         basename, extension =
os.path.splitext(fullPath)

        
if extension == ".mxd":

             mxd =
arcpy.mapping.MapDocument(fullPath)

            
print "MXD: " + fileName

            


             #get the list of all the layers

            
brknList = arcpy.mapping.ListLayers(mxd)



            
# Remove temporary connection file if it already exists

            
sdeFile = r"C:\Project\Output\TempConnection.sde"

            
if os.path.exists(sdeFile):

                
print sdeFile

                 os.remove(sdeFile)



           
#replace data source of individual SDE layer

            
for brknItem in brknList:

                
if brknItem.datasetName == 'GISADMIN.ABC_Stations':

                   
                     brknItem.replaceDataSource(wrkspcInfra,
"SDE_WORKSPACE",newStation, False)

              #elif brknItem.datasetName == 'GISADMIN.ABC_Layer':

 

              #     brknItem.replaceDataSource(wrkspcInfra2, "SDE_WORKSPACE",newABCLayer, False)

 

 

 

          

            
outmxd = basename + "_3"+ extension

             mxd.saveACopy(outmxd)

             arcpy.RefreshTOC()

             arcpy.RefreshActiveView()

 

--------------------------------------------------------------------------------------------------------

 

Outcomes