<?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: How to programmatically change the data source for a query layer? in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537181#M88129</link>
    <description>&lt;P&gt;I'm fairly certain there isn't a way to modify query layer data sources in ArcPy.&amp;nbsp; Although it was some years ago now, I had this come up on a project in the ArcMap days and I had to write an ArcObjects script to modify query layers in an MXD en masse, there was no ArcPy route available.&amp;nbsp; As far as I know that's still the case, except ArcObjects aren't available in Pro.&lt;/P&gt;&lt;P&gt;However ... In the ArcMap days layer files were binary, and nowadays layer files (LYRX) are XML, so &lt;EM&gt;theoretically&lt;/EM&gt; you could save your query layer as a LYRX and use some XML editing code to update it that way.&amp;nbsp; I haven't done this but it sounds good in theory, haha.&lt;/P&gt;</description>
    <pubDate>Wed, 11 Sep 2024 03:56:43 GMT</pubDate>
    <dc:creator>MobiusSnake</dc:creator>
    <dc:date>2024-09-11T03:56:43Z</dc:date>
    <item>
      <title>How to programmatically change the data source for a query layer?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537106#M88116</link>
      <description>&lt;P&gt;We are trying to change the data sources&amp;nbsp; for multiple query layers in multiple aprx files. We have run a test script that uses a template aprx and successfully changes the data source through the layer connection properties. However, when opening the new aprx, no data appears.&lt;/P&gt;&lt;P&gt;Once we open the properties for the query layer and make any edits to the query, it then "auto-validates" and data populates. This manual step of editing the query to prompt the query layer to "validate" defeats the purpose of scripting the data source change.&lt;/P&gt;&lt;P&gt;Does anybody have any tips on how to get the query layer to "auto-validate"/self-validate/run through whatever check it's doing before populating data?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
aprx = arcpy.mp.ArcGISProject(r'path\to\file.aprx')
all_maps = aprx.listMaps()
for m in all_maps:
    all_layers = m.listLayers()
    for lyr in all_layers: 
        if not lyr.isGroupLayer: 
            try:
                cp = lyr.connectionProperties
                cp['connection_info']['database'] = 'db123'
                lyr.updateConnectionProperties(lyr.connectionProperties, cp)
            except: 
                print("Not Included: " + lyr.name)       
aprx.saveACopy(r"path\to\new\file.aprx")&lt;/LI-CODE&gt;&lt;P&gt;We've also thought about starting from a blank aprx and using Make Query Layers to populate each of them. However, this function requires the layer to be copied to a feature class or other static item. Is there any way to persist the Query Layer in an aprx map?&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2024 21:27:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537106#M88116</guid>
      <dc:creator>Map12</dc:creator>
      <dc:date>2024-09-10T21:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to programmatically change the data source for a query layer?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537181#M88129</link>
      <description>&lt;P&gt;I'm fairly certain there isn't a way to modify query layer data sources in ArcPy.&amp;nbsp; Although it was some years ago now, I had this come up on a project in the ArcMap days and I had to write an ArcObjects script to modify query layers in an MXD en masse, there was no ArcPy route available.&amp;nbsp; As far as I know that's still the case, except ArcObjects aren't available in Pro.&lt;/P&gt;&lt;P&gt;However ... In the ArcMap days layer files were binary, and nowadays layer files (LYRX) are XML, so &lt;EM&gt;theoretically&lt;/EM&gt; you could save your query layer as a LYRX and use some XML editing code to update it that way.&amp;nbsp; I haven't done this but it sounds good in theory, haha.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 03:56:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537181#M88129</guid>
      <dc:creator>MobiusSnake</dc:creator>
      <dc:date>2024-09-11T03:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to programmatically change the data source for a query layer?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537677#M88185</link>
      <description>&lt;P&gt;Idea:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/modify-query-layer-data-source-using-python/idi-p/1537676/highlight/true" target="_self"&gt;Modify query layer data source using Python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 10:09:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-to-programmatically-change-the-data-source-for/m-p/1537677#M88185</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-09-12T10:09:01Z</dc:date>
    </item>
  </channel>
</rss>

