Select to view content in your preferred language

Iterate Through MXDs to Change One Layer Data Source for Multiple MXDs

2765
13
03-18-2021 03:33 PM
rnaneliu
Emerging Contributor

Hi,  I am trying to change the data source of our parcel fabric that is not being maintained to a new one stored in the same SDE dataset.  I have created a script to the best of my ability and I get this error.  I have searched for possible solutions all day.  I am sure it's a simple solution but this is time sensitive and I need to figure out what I am doing wrong as soon as possible.  Thanks for any help you can offer. 

ERROR:

if lyr.name == "Fernie Parcel":
NameError: name 'lyr' is not defined

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

import arcpy
from arcpy import env
from arcpy import mapping
arcpy.env.workspace = workspace = r"P:\mxd_data_source"

arcpy.env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
mxd = workspace + "\\" + mxd
print mxd + " is being processed"
mxd = arcpy.mapping.MapDocument(mxd)
for df in arcpy.mapping.ListDataFrames(mxd, "*"):
if lyr.name == "Fernie Parcel":
lyr.replaceDataSource(workspace, "SDE_WORKSPACE", "parcel_pmbc", validate=True)
print "Successfully updated data sources"
mxd.save()

0 Kudos
13 Replies
by Anonymous User
Not applicable

Yes, that was my fault for using workspace like that.  By checking the workspace I was meaning the datasource string such as 'CF on SQL-DEFAULT.sde\CF.SDE.Base' but that might not be necessary. 

What are the two layer names?

0 Kudos
rnaneliu
Emerging Contributor

Hi Jeff, 

No worries, appreciate the help.  The layer names are CF.SDE.parcel and CF.SDE.parcel pmbc. Cheers,

0 Kudos
by Anonymous User
Not applicable

ok- it wasn't getting that second layer because I don't think we were telling it to.  From the docs for the data_set parameter:

A string that represents the name of the dataset the way it appears in the new workspace (not the name of the layer in the TOC). If dataset_name is not provided, the replaceDataSource method will attempt to replace the dataset by finding a table with a the same name as the layer's current dataset property.

So we could try:

arcpy.env.workspace = workspace = r"P:\mxd_data_source"
pathToSDEConnection = r'Path to your connection file'

# arcpy.env.overwriteOutput = True
mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxdpath = workspace + "\\" + mxd
    print mxd + " is being processed"

    mxdMd = arcpy.mapping.MapDocument(mxdpath)

    for df in arcpy.mapping.ListDataFrames(mxdMd, "*"):
        for lyr in arcpy.mapping.ListLayers(df):
            print lyr.name
            if lyr.name == "Fernie Parcel":
                arcpy.AddMessage("Updating connections: {}".format(lyr.name))
                lyr.replaceDataSource(pathToSDEConnection, "SDE_WORKSPACE", "parcel_pmbc", validate=True)
                lyr.replaceDataSource(pathToSDEConnection, "SDE_WORKSPACE", "parcel", validate=True)
    mxdMd.save()
    print("Done updating data sources in {}".format(mxd))

 

0 Kudos
rnaneliu
Emerging Contributor

Thanks Jeff.  I ran the script. It went through all of the mxds but didn't change the source.  My email is sajegis@gmail.com. I would love to fix this problem but it just could be on our side.

Cheers,

0 Kudos