<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy.replaceDataSource in an mxd in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1026751#M59975</link>
    <description>&lt;P&gt;&lt;STRIKE&gt;Would it be possible for you to format your code to make it easier and to check for syntax errors.&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;Just saw the plea for code guidance.... here you have it... expand the &lt;STRONG&gt;. . .&amp;nbsp;&lt;/STRONG&gt;as in the link&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank" rel="noopener"&gt;Code formatting ... the Community Version - GeoNet, The Esri Community&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Feb 2021 03:17:40 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2021-02-15T03:17:40Z</dc:date>
    <item>
      <title>arcpy.replaceDataSource in an mxd</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1026746#M59972</link>
      <description>&lt;P&gt;I have an mxd file with about 80 layers. Each layer has to be updated with a standard symbology and labeling. If I use the arcpy.ApplySymbologyFromLayer_management it only updates the symbology. I want to up update &lt;EM&gt;everything&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;When I use the&amp;nbsp;arcpy.mapping.UpdateLayer&amp;nbsp; function it does exactly what I want. However it changes the layer name and the data source. I'm trying to run UpdateLayer and then change the name and datasource back.&lt;/P&gt;&lt;P&gt;Unfortunately when I run Layer.replaceDataSource for a layer it changes &lt;EM&gt;all&lt;/EM&gt; the layers in the map.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code is posted raw because I can't find the Code tag....&lt;/P&gt;&lt;P&gt;import arcpy, os&lt;/P&gt;&lt;P&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;BR /&gt;df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]&lt;BR /&gt;layers = arcpy.mapping.ListLayers(mxd)&lt;BR /&gt;# Master featureclass names&lt;BR /&gt;PolylineMaster = 'PolylineMaster'&lt;BR /&gt;PointMaster = 'PointMaster'&lt;BR /&gt;PolygonMaster = 'PolygonMaster'&lt;BR /&gt;# Run each of point, polyline or polygon&lt;BR /&gt;doPoint = False&lt;BR /&gt;doPolyline = True&lt;BR /&gt;doPolygon = False&lt;/P&gt;&lt;P&gt;if doPoint == True:&lt;BR /&gt;lyrPtMaster = arcpy.mapping.ListLayers(mxd,PointMaster,df)[0]&lt;/P&gt;&lt;P&gt;if doPolyline == True:&lt;BR /&gt;lyrLineMaster = arcpy.mapping.ListLayers(mxd,PolylineMaster,df)[0]&lt;/P&gt;&lt;P&gt;if doPolygon == True:&lt;BR /&gt;lyrPolyMaster = arcpy.mapping.ListLayers(mxd,PolygonMaster,df)[0]&lt;/P&gt;&lt;P&gt;for lyr in layers:&lt;BR /&gt;lyrName = lyr.name&lt;BR /&gt;&lt;BR /&gt;if lyrName == PolylineMaster or lyrName == PointMaster or lyrName == PolygonMaster:&lt;BR /&gt;continue&lt;BR /&gt;else:&lt;BR /&gt;print(lyrName)&lt;BR /&gt;desc = arcpy.Describe(lyr)&lt;BR /&gt;#path = arcpy.Describe(lyr).catalogPath # Full path - doesn't work&lt;/P&gt;&lt;P&gt;path = arcpy.Describe(lyr).path # GDB path&lt;BR /&gt;print(path)&lt;BR /&gt;if desc.shapeType == "Point" and doPoint == True:&lt;BR /&gt;arcpy.mapping.UpdateLayer(df,lyr,lyrPtMaster,False)&lt;BR /&gt;elif desc.shapeType == "Polyline" and doPolyline == True:&lt;BR /&gt;arcpy.mapping.UpdateLayer(df,lyr,lyrLineMaster,False)&lt;BR /&gt;elif desc.shapeType == "Polygon" and doPolygon == True:&lt;BR /&gt;arcpy.mapping.UpdateLayer(df,lyr,lyrPolyMaster,False)&lt;/P&gt;&lt;P&gt;# Reset the data source and the layer name - both of which have been changed to the Master&lt;BR /&gt;lyr.replaceDataSource(path,'FILEGDB_WORKSPACE',lyrName,True)&lt;BR /&gt;print(lyrName)&lt;BR /&gt;lyr.name = lyrName&lt;BR /&gt;print(lyr.name)&lt;BR /&gt;arcpy.RefreshActiveView()&lt;BR /&gt;arcpy.RefreshTOC()&lt;BR /&gt;del mxd&lt;/P&gt;&lt;P&gt;I am using ArcGIS 10.6&lt;/P&gt;&lt;P&gt;Can replaceDataSource be used to replace just one layer's data source?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 02:14:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1026746#M59972</guid>
      <dc:creator>JohnMcGlynn</dc:creator>
      <dc:date>2021-02-15T02:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.replaceDataSource in an mxd</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1026751#M59975</link>
      <description>&lt;P&gt;&lt;STRIKE&gt;Would it be possible for you to format your code to make it easier and to check for syntax errors.&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;Just saw the plea for code guidance.... here you have it... expand the &lt;STRONG&gt;. . .&amp;nbsp;&lt;/STRONG&gt;as in the link&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank" rel="noopener"&gt;Code formatting ... the Community Version - GeoNet, The Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 03:17:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1026751#M59975</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-15T03:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.replaceDataSource in an mxd</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1027383#M59992</link>
      <description>&lt;P&gt;Thanks Dan. This looks much better.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
layers = arcpy.mapping.ListLayers(mxd)
# Master featureclass names
PolylineMaster = 'PolylineMaster'
PointMaster = 'PointMaster'
PolygonMaster = 'PolygonMaster'
# Run each of point, polyline or polygon
doPoint = False
doPolyline = True
doPolygon = False

if doPoint == True:
    lyrPtMaster = arcpy.mapping.ListLayers(mxd,PointMaster,df)[0]

if doPolyline == True:
    lyrLineMaster = arcpy.mapping.ListLayers(mxd,PolylineMaster,df)[0]

if doPolygon == True:
    lyrPolyMaster = arcpy.mapping.ListLayers(mxd,PolygonMaster,df)[0]

for lyr in layers:
    lyrName = lyr.name
   
    if lyrName == PolylineMaster or lyrName == PointMaster or lyrName == PolygonMaster:
        continue
    else:
        print(lyrName)
        desc = arcpy.Describe(lyr)
        path = arcpy.Describe(lyr).catalogPath
        print(path)
        if desc.shapeType == "Point" and doPoint == True:
            arcpy.mapping.UpdateLayer(df,lyr,lyrPtMaster,False)
        elif desc.shapeType == "Polyline" and doPolyline == True:
            arcpy.mapping.UpdateLayer(df,lyr,lyrLineMaster,False)
        elif desc.shapeType == "Polygon" and doPolygon == True:
            arcpy.mapping.UpdateLayer(df,lyr,lyrPolyMaster,False)

        # Reset the data source and the layer name - both of which have been changed to the Master
        lyr.replaceDataSource(path,'FILEGDB_WORKSPACE',lyrName,True)
        print(lyrName)
        lyr.name = lyrName
        print(lyr.name)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 22:51:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1027383#M59992</guid>
      <dc:creator>JohnMcGlynn</dc:creator>
      <dc:date>2021-02-16T22:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.replaceDataSource in an mxd</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1028864#M60038</link>
      <description>&lt;P&gt;I noticed in the examples they are saving the updated mxds as a copy.&amp;nbsp; Have you tried doing that and seeing if things are persisted?&lt;/P&gt;&lt;P&gt;I vaguely remember fixing some broken datasources in mxds a few years ago.&amp;nbsp; I can try to find the script and share it with you for some ideas but I think replaceDataSource didn't work and I ended up just replacing the layers path to make it stick.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Feb 2021 18:24:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-replacedatasource-in-an-mxd/m-p/1028864#M60038</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-02-20T18:24:41Z</dc:date>
    </item>
  </channel>
</rss>

