<?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 replaceDataSource in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496883#M39038</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have mxd i work on for different cases, but have the the same layers. The script i am working on needs to update the "Subject_Property" layer data source. I am having trouble getting this to work. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script will search for a text in the "DXF_TEXT" filed and export it out to the location of the mxd. The mxd already has the layer "SUBJECT_PROPERTY" but i need to get it to update the mxd's "SUBJECT_PROPERTY" with the one that was searched and exported,&amp;nbsp; don't needed added to the current mxd. hopefully this makes sense.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code i have so far. Any help would be gratefully appreciated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import arcpy, string, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)



value = arcpy.GetParameterAsText(0)
if value == "#" or value == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp; value = None
if value:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"DXF_TEXT\" = '" + str(value) +"'")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("no value")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
df.extent = lyr.getSelectedExtent()
df.scale = 7000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) &amp;gt; 0:

&amp;nbsp;&amp;nbsp; arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

#del mxd
#mxd = arcpy.mapping.MapDocument("CURRENT")
#lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath
data_path = os.path.dirname(lyrpath)

SP = "SUBJECT_PROPERTY" 
lyr.replaceDataSource(data_path, "SHAPEFILE_WORKSPACE", SP, True )

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 05 Mar 2013 17:15:35 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2013-03-05T17:15:35Z</dc:date>
    <item>
      <title>replaceDataSource</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496883#M39038</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have mxd i work on for different cases, but have the the same layers. The script i am working on needs to update the "Subject_Property" layer data source. I am having trouble getting this to work. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script will search for a text in the "DXF_TEXT" filed and export it out to the location of the mxd. The mxd already has the layer "SUBJECT_PROPERTY" but i need to get it to update the mxd's "SUBJECT_PROPERTY" with the one that was searched and exported,&amp;nbsp; don't needed added to the current mxd. hopefully this makes sense.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code i have so far. Any help would be gratefully appreciated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import arcpy, string, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)



value = arcpy.GetParameterAsText(0)
if value == "#" or value == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp; value = None
if value:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"DXF_TEXT\" = '" + str(value) +"'")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("no value")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
df.extent = lyr.getSelectedExtent()
df.scale = 7000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) &amp;gt; 0:

&amp;nbsp;&amp;nbsp; arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

#del mxd
#mxd = arcpy.mapping.MapDocument("CURRENT")
#lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath
data_path = os.path.dirname(lyrpath)

SP = "SUBJECT_PROPERTY" 
lyr.replaceDataSource(data_path, "SHAPEFILE_WORKSPACE", SP, True )

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Mar 2013 17:15:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496883#M39038</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2013-03-05T17:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496884#M39039</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have something that works but i am not sure if this is correct or if there is an easier way of doing this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any input would be gratefully appreciated please.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, string, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)



value = arcpy.GetParameterAsText(0)
if value == "#" or value == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp; value = None
if value:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"DXF_TEXT\" = '" + str(value) +"'")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("no value")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
df.extent = lyr.getSelectedExtent()
df.scale = 24000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) &amp;gt; 0:

&amp;nbsp;&amp;nbsp; arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

del mxd, wp, 
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath
#data_path = os.path.dirname(lyrpath)
wp = os.path.dirname(mxd.filePath)

SP = "SUBJECT_PROPERTY" 
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:50:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496884#M39039</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2021-12-11T21:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: replaceDataSource</title>
      <link>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496885#M39040</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm not clear about your concern (if it is working).&amp;nbsp; As long as your wp variable is pointing to the correct location, you have the workspace type correct and you have a valid data source, it should just work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Mar 2013 16:46:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacedatasource/m-p/496885#M39040</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2013-03-08T16:46:56Z</dc:date>
    </item>
  </channel>
</rss>

