<?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 ArcGIS Pro query layer loses selected workspace in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1188408#M56808</link>
    <description>&lt;P&gt;I have a number of query layers in my ArcGIS Pro map. When I try and perform and arcpy code on these query layers it loses the connection to the workspace.&lt;/P&gt;&lt;P&gt;A simple task I am trying to do is update the Display Field for all these query layers (see code below).&lt;/P&gt;&lt;P&gt;When I run this, I get the datasource missing red exclamation mark next to my query layers. When I go to the Source in the layer's Properties and view the query, the connection to my enterprise gdb is still showing there but the code syntax colours are not shown (only white text) (I also can't select the List of Tables - ERROR: The selected workspace is invalid):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="julian_svcs_0-1656611473468.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/44918i41633FFBF7F2E99A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="julian_svcs_0-1656611473468.png" alt="julian_svcs_0-1656611473468.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;If I select another database connection, then reselect my correct connection it restores the connection again:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="julian_svcs_1-1656611600030.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/44919i5A30B9C888C9307F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="julian_svcs_1-1656611600030.png" alt="julian_svcs_1-1656611600030.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Why does it loose the database connection? I checked the CIM in an exported lyrx file for both and the connections seem to be the same.&lt;/P&gt;&lt;P&gt;Is there at least a way I can automate the connection re-connection with python instead of having to do the above for every layer?&lt;/P&gt;&lt;P&gt;Python code to update the displayField:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;aprx = arcpy.mp.ArcGISProject("CURRENT")
for m in aprx.listMaps():
    for lyr in m.listLayers():
        if lyr.isFeatureLayer:
            cim_lyr = lyr.getDefinition("V2")
            cim_lyr.featureTable.displayField = "NAME"
            lyr.setDefinition(cim_lyr)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;Julian.&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jun 2022 18:00:12 GMT</pubDate>
    <dc:creator>julian_svcs</dc:creator>
    <dc:date>2022-06-30T18:00:12Z</dc:date>
    <item>
      <title>ArcGIS Pro query layer loses selected workspace</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1188408#M56808</link>
      <description>&lt;P&gt;I have a number of query layers in my ArcGIS Pro map. When I try and perform and arcpy code on these query layers it loses the connection to the workspace.&lt;/P&gt;&lt;P&gt;A simple task I am trying to do is update the Display Field for all these query layers (see code below).&lt;/P&gt;&lt;P&gt;When I run this, I get the datasource missing red exclamation mark next to my query layers. When I go to the Source in the layer's Properties and view the query, the connection to my enterprise gdb is still showing there but the code syntax colours are not shown (only white text) (I also can't select the List of Tables - ERROR: The selected workspace is invalid):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="julian_svcs_0-1656611473468.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/44918i41633FFBF7F2E99A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="julian_svcs_0-1656611473468.png" alt="julian_svcs_0-1656611473468.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;If I select another database connection, then reselect my correct connection it restores the connection again:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="julian_svcs_1-1656611600030.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/44919i5A30B9C888C9307F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="julian_svcs_1-1656611600030.png" alt="julian_svcs_1-1656611600030.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Why does it loose the database connection? I checked the CIM in an exported lyrx file for both and the connections seem to be the same.&lt;/P&gt;&lt;P&gt;Is there at least a way I can automate the connection re-connection with python instead of having to do the above for every layer?&lt;/P&gt;&lt;P&gt;Python code to update the displayField:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;aprx = arcpy.mp.ArcGISProject("CURRENT")
for m in aprx.listMaps():
    for lyr in m.listLayers():
        if lyr.isFeatureLayer:
            cim_lyr = lyr.getDefinition("V2")
            cim_lyr.featureTable.displayField = "NAME"
            lyr.setDefinition(cim_lyr)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;Julian.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 18:00:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1188408#M56808</guid>
      <dc:creator>julian_svcs</dc:creator>
      <dc:date>2022-06-30T18:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro query layer loses selected workspace</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1188525#M56839</link>
      <description>&lt;P&gt;I think I found the issue with this. When I run the display field and same the layers' definition, the cim loses the 'queryField' and 'featureTemplates' values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a workaround for now:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;create a lyrx file&lt;/LI&gt;&lt;LI&gt;update the display field&lt;/LI&gt;&lt;LI&gt;update the queryFields and featureTemplates using the key, values from the lyrx file&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This seems to restore the workspace connection.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 23:48:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1188525#M56839</guid>
      <dc:creator>julian_svcs</dc:creator>
      <dc:date>2022-06-30T23:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro query layer loses selected workspace</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1248044#M64235</link>
      <description>&lt;P&gt;Hi Julian,&lt;/P&gt;&lt;P&gt;I have the same issue using CIM v2 to alter datasources on query layers. This method drops some information necessary for the workspace connection. The only way I've been able to find the information is using the lyrx file like you said to restore the queryFields.&lt;/P&gt;&lt;P&gt;This is a bummer because CIM is the only efficient way to automate changing datasources on APRX files with lots of layers, and it is not practical to maintain a copy of the queryFields for every layer needed. It would be helpful if anyone at ESRI knows if this a bug or expected behavior.&lt;/P&gt;&lt;P&gt;-Mick&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 14:34:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1248044#M64235</guid>
      <dc:creator>MickHuska-NAU</dc:creator>
      <dc:date>2023-01-13T14:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro query layer loses selected workspace</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1346982#M74958</link>
      <description>&lt;P&gt;This is has been fixed ArcGIS Pro version 3.2. In the Updating Data Sources help topic, go to the CIM Section, and check out the third code sample:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Reference project, map and layer 
p = arcpy.mp.ArcGISProject(r'C:\Projects\USA.aprx')
m = p.listMaps('USA')[0]
l = m.listLayers('States')[0]

# Get the layer's CIM definition
lyrCIM = l.getDefinition('V3')         

# Update the sql query where clause for the layer
sql = lyrCIM.featureTable.dataConnection.sqlQuery
newsql = sql.replace("WHERE SUB_REGION = 'Mtn'", "WHERE SUB_REGION = 'Pacific'")
lyrCIM.featureTable.dataConnection.sqlQuery = newsql
    
# Set the layer's CIM definition
l.setDefinition(lyrCIM)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above sample changes the sql query, but other properties of the Query Layer, such as the Display Field, can also be updated using similar logic.&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;</description>
      <pubDate>Wed, 08 Nov 2023 01:26:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcgis-pro-query-layer-loses-selected-workspace/m-p/1346982#M74958</guid>
      <dc:creator>JeffMoulds</dc:creator>
      <dc:date>2023-11-08T01:26:05Z</dc:date>
    </item>
  </channel>
</rss>

