ReplaceDataSource of SDE layers in map documents (mxd) with old feature classes newly organized under feature datasets in the same ArcSDE

853
1
07-18-2016 11:14 AM
Pyung_HoKim
New Contributor

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()

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

0 Kudos
1 Reply
AllenGuan1
New Contributor II

The above layer.replaceDataSource(targetworkspace, "SDE_WORKSPACE",newDataset, False) works perfect when the newDataset is a SDE feature class. But it does not work with feature dataset\featureclass. For example, if I want to reset data source from sde1.sde\OWNER1.featuredataset\OWNER1.featureclass to sde2.sde\OWNER2.featuredataset\OWNER2.featureclass, the function does not work:

layer.replaceDataSource(sde2.sde, "SDE_WORKSPACE","OWNER2.featuredataset\OWNER2.featureclass, False) . It changes the target sde worpsace to sde2.sde, but does not change the dataset owner to be OWNER2.  Any idea how can I reset featuredataset\featureclass to be in different workspace and with a different owner.

0 Kudos