<?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: Change layer source in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501213#M39360</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It looks like your new path:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;newPath = r"Z:\Working\Export_Output.shp" &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;is the full path, while the workspace_path parameter of the replaceDataSource method requires directory name (not the file name too).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The data source method requires the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;replaceDataSource (workspace_path, workspace_type, {dataset_name}, {validate})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;layer.replaceDataSource(os.path.dirname(newPath), "SHAPEFILE_WORKSPACE", os.path.basename(newpath))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 17 Jun 2014 17:30:51 GMT</pubDate>
    <dc:creator>MattEiben</dc:creator>
    <dc:date>2014-06-17T17:30:51Z</dc:date>
    <item>
      <title>Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501211#M39358</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am working on a script that will walk a directory and change the source for a layer. This way when we move data I can just set this script to go update everyone's maps. In my own folder I can get it to run down to printing the layer source, I just can't get the replaceDataSource function to work. Any help? This is in 10.1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also will I be able to replace a source to something that is loaded from a group or basemap layer?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#import site-packages, modules and set enviroment
import arcpy, os

#initialize variables
extList = ["mxd"]
mappaths = []

targetDir = r"Z:\Working\MXDs" 
oldPath = r"Z:\Working\MXDs\Export_Output.shp" 
newPath = r"Z:\Working\Export_Output.shp" 


#Loop through each MXD in the folder and writes path to a list
for root, dirs, files in os.walk(targetDir):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; splitFilename = filename.split(".")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if splitFilename[-1] in extList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fullpath = os.path.join(root, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mappaths.append(fullpath)

#prints list
for maps in mappaths:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print maps
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(maps)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if layer.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if layer.dataSource == oldPath:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Layer: " + layer.name + "\n&amp;nbsp; Source: " + layer.dataSource
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.replaceDataSource(newPath, "NONE")

print "End Script"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Results when running the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Z:\Working\MXDs\BrkLyr_Map_2.mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Layer: Export_Output&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Source: \Working\MXDs\Export_Output.shp&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "Z:\Utilities\Python\Wes PYSCRIPTS\Tests\FindBrokenLayers_2for.py", line 41, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; layer.replaceDataSource(newPath, "NONE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; return fn(*args, **kw)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ValueError: Layer: Unexpected error&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:33:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501211#M39358</guid>
      <dc:creator>WesleyStreet</dc:creator>
      <dc:date>2021-12-12T16:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501212#M39359</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Its likely because you need to specify the workspace path(the folder the file is in, not the file) with the first parameter, and then the workspace type (optional) then dataset name(optional) then validate(optional).&amp;nbsp; If you give the workspace in the first parameter, and not the file itself, it will probably work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So it would be &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;newpath =&amp;nbsp; r"Z:\Working"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;layer.replaceDataSource(newPath, "None")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jun 2014 17:27:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501212#M39359</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2014-06-17T17:27:52Z</dc:date>
    </item>
    <item>
      <title>Re: Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501213#M39360</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It looks like your new path:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;newPath = r"Z:\Working\Export_Output.shp" &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;is the full path, while the workspace_path parameter of the replaceDataSource method requires directory name (not the file name too).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The data source method requires the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;replaceDataSource (workspace_path, workspace_type, {dataset_name}, {validate})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;layer.replaceDataSource(os.path.dirname(newPath), "SHAPEFILE_WORKSPACE", os.path.basename(newpath))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jun 2014 17:30:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501213#M39360</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2014-06-17T17:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501214#M39361</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I tired iamuarry's and it ran with no error but didn't fix the layer source.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried eibemn's solution and I am getting the same error.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jun 2014 17:36:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501214#M39361</guid>
      <dc:creator>WesleyStreet</dc:creator>
      <dc:date>2014-06-17T17:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501215#M39362</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;May be worth trying mxd.findAndReplaceWorkspacePaths (find_workspace_path, replace_workspace_path, {validate}) .&amp;nbsp; Then you wouldn't have to loop through the layers either.&amp;nbsp; Also, for your walking code, you could save a couple of steps and use the string.endswith() method.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#Loop through each MXD in the folder and writes path to a list
for root, dirs, files in os.walk(targetDir):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if filename.endswith(".mxd")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #append to list
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:00:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501215#M39362</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2021-12-11T22:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Change layer source</title>
      <link>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501216#M39363</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well most of my code was correct.&amp;nbsp; It was the maps that were somehow messed up.&amp;nbsp; I don't know if my process of building and correcting this script corrupted the maps or what.&amp;nbsp; I made 3 new test maps and it ran fine using replaceDataSource and findAndReplaceWorkspacePath.&amp;nbsp; Also I forgot the mxd.save() and del mxd at the end of my for loop.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jun 2014 14:13:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/change-layer-source/m-p/501216#M39363</guid>
      <dc:creator>WesleyStreet</dc:creator>
      <dc:date>2014-06-25T14:13:03Z</dc:date>
    </item>
  </channel>
</rss>

