<?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: unable to change lyrx source using arcpy in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192213#M57236</link>
    <description>&lt;P&gt;As lyrx files are just json, you could just change the source in the line of json and save the file, without even touching arcpy or an aprx.&lt;BR /&gt;&lt;BR /&gt;Just fill in the path to the layer file for &lt;EM&gt;filename&lt;/EM&gt; and the path to the new folder location for the data for &lt;EM&gt;newPath&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
filename = r''
newPath = r''

with open(filename, "r") as file:
    data = json.load(file)
    data["layerDefinitions"][0]["featureTable"]["dataConnection"]["workspaceConnectionString"] = newPath

with open(filename, "w") as file:
    json.dump(data, file)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jul 2022 15:00:33 GMT</pubDate>
    <dc:creator>RichardHowe</dc:creator>
    <dc:date>2022-07-14T15:00:33Z</dc:date>
    <item>
      <title>unable to change lyrx source using arcpy</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192138#M57228</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have lyrx files in a template directory. The lyrx files displaydata from shapefiles form various folders (all in the same path).&lt;/P&gt;&lt;P&gt;I want to&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;copy the lyrx's across to a new project directory (no problems here)&lt;/LI&gt;&lt;LI&gt;change the source of the shapefiles the lyrx's reference.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I'm using Python3, and arcpy.mp&lt;/P&gt;&lt;P&gt;Method #1: I have tried to do achieve #2in the map document using .updateConnectionProperties, the script runs but there's no source changes.&lt;/P&gt;&lt;P&gt;Method #2 (Preferred): I've also tried to manipulate the lyrx files directly - this is my preferred method as there is no MXD required (I think?) but still no luck changing the source. And no errors, everything seems to be running fine.&lt;/P&gt;&lt;P&gt;The lyrx I'm working with are groups; ie BareMap.lyrx contains a number of layers itself. I don't know if I should be separating them, manipulating the data and then grouping them?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="justin77_0-1657785276131.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45863i8B86BEF2D3E562FF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="justin77_0-1657785276131.png" alt="justin77_0-1657785276131.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The code I'm using is modified slightly from this post&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/change-data-source-of-lyrx-files-in-bulk-and/td-p/1037274" target="_blank"&gt;Solved: Change Data Source of .lyrx files in bulk and perm... - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;filesDirectory = path.join(destination_dir, "new_layers")
fileExt = r".lyrx"
files = [path.join(filesDirectory, f) for f in listdir(filesDirectory) if f.endswith(fileExt)]
ticker = 0
fileExt = r".lyrx" #do not change it
files = [path.join(filesDirectory, f) for f in listdir(filesDirectory) if f.endswith(fileExt)] #do not change it
for file in files:
    lyrFile = arcpy.mp.LayerFile(file)
    lyrFile.updateConnectionProperties(r"C:\Users\pc9Code\ndc\toolbox\Data", r"C:\Users\pc9\newcode") #path to old database and path to new database (.sde file in your case)
    lyrFile.saveACopy(file.split(".")[0]+ "New.lyrx")
    ticker += 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Would love any suggestions it's doing my head in &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 07:56:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192138#M57228</guid>
      <dc:creator>justin77</dc:creator>
      <dc:date>2022-07-14T07:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: unable to change lyrx source using arcpy</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192213#M57236</link>
      <description>&lt;P&gt;As lyrx files are just json, you could just change the source in the line of json and save the file, without even touching arcpy or an aprx.&lt;BR /&gt;&lt;BR /&gt;Just fill in the path to the layer file for &lt;EM&gt;filename&lt;/EM&gt; and the path to the new folder location for the data for &lt;EM&gt;newPath&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
filename = r''
newPath = r''

with open(filename, "r") as file:
    data = json.load(file)
    data["layerDefinitions"][0]["featureTable"]["dataConnection"]["workspaceConnectionString"] = newPath

with open(filename, "w") as file:
    json.dump(data, file)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 15:00:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192213#M57236</guid>
      <dc:creator>RichardHowe</dc:creator>
      <dc:date>2022-07-14T15:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: unable to change lyrx source using arcpy</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192406#M57264</link>
      <description>&lt;P&gt;thankyou!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 04:43:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/unable-to-change-lyrx-source-using-arcpy/m-p/1192406#M57264</guid>
      <dc:creator>justin77</dc:creator>
      <dc:date>2022-07-15T04:43:12Z</dc:date>
    </item>
  </channel>
</rss>

