I am trying to create a Map Tool where it only selects features in my "Parcels" layer. Some users will have the "Parcels" layer inside a Group Layer in the Map and some will not.
.NET C#
<SPAN class="keyword token">var</SPAN> parcellayer <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN>Layers<SPAN class="punctuation token">.</SPAN><SPAN class="token function">First</SPAN><SPAN class="punctuation token">(</SPAN>layer <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> layer<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Equals</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Parcels"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> FeatureLayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
This works well if the layer is directly in the Map, but will crash if the layer is in a Group Layer in the Map.
I have yet to figure out how to use a layer within a Group Layer in the same manner, simply because it is not what I am after.
According to ProConcepts Map Authoring · Esri/arcgis-pro-sdk Wiki · GitHub , Working with Map Members, one should use FlattenedList because "Should you need to get a list without group layers hierarchy, use Map.GetLayersAsFlattenedList() method".
When I do this, nothing happens when I try and use the new Map Tool
.NET C#
<SPAN class="keyword token">var</SPAN> parcellayer <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetLayersAsFlattenedList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">Where</SPAN><SPAN class="punctuation token">(</SPAN>layer <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> layer<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Equals</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Parcels"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> FeatureLayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
I want to be able to make a Select Map Tool where it will select the "Parcels" layer regardless of if it is in a Group Layer or not.
To add more confusion. I can run a stand alone Python Script within Pro and it will not matter if a feature layer is in a Group layer or not.
Python
selectPar <SPAN class="operator token">=</SPAN> pm<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Parcels"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"WITHIN_A_DISTANCE"</SPAN><SPAN class="punctuation token">,</SPAN>ZoneCaseFeat<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"1000 Feet"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
But if I run this same exact Python script from within .NET I have to put the Group in there first.
Python
selectPar <SPAN class="operator token">=</SPAN> pm<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"GroupLayerName\Parcels"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"WITHIN_A_DISTANCE"</SPAN><SPAN class="punctuation token">,</SPAN>ZoneCaseFeat<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"1000 Feet"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
But if I do this and run the Python script from within .Net it will work fine in or out of a group!
Python
aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
m <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
ParcelsLayer <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Parcels"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
selectPar <SPAN class="operator token">=</SPAN> pm<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation<SPAN class="punctuation token">(</SPAN>ParcelsLayer<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"WITHIN_A_DISTANCE"</SPAN><SPAN class="punctuation token">,</SPAN>ZoneCaseFeat<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"1000 Feet"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>