<?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 List of layers TOC preserving nesting in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1203041#M8591</link>
    <description>&lt;P&gt;Is there a way to loop through the TOC and preserve the nested hierarchy i.e. NOT the&amp;nbsp;&lt;SPAN&gt;GetLayersAsFlattenedList() method. I also need to examine whether they are group layers or feature/raster layers as I go round.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Can't find anything obvious. I did try:&lt;/P&gt;&lt;P&gt;MapView.Active.Map.Layers&lt;/P&gt;&lt;P&gt;but that just returned me the project node, an item count of 1 - when I had numerous layers loaded in, that made no sense to me.&lt;/P&gt;&lt;P&gt;If anyone can help that would be great.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Aug 2022 20:21:12 GMT</pubDate>
    <dc:creator>Vidar</dc:creator>
    <dc:date>2022-08-15T20:21:12Z</dc:date>
    <item>
      <title>List of layers TOC preserving nesting</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1203041#M8591</link>
      <description>&lt;P&gt;Is there a way to loop through the TOC and preserve the nested hierarchy i.e. NOT the&amp;nbsp;&lt;SPAN&gt;GetLayersAsFlattenedList() method. I also need to examine whether they are group layers or feature/raster layers as I go round.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Can't find anything obvious. I did try:&lt;/P&gt;&lt;P&gt;MapView.Active.Map.Layers&lt;/P&gt;&lt;P&gt;but that just returned me the project node, an item count of 1 - when I had numerous layers loaded in, that made no sense to me.&lt;/P&gt;&lt;P&gt;If anyone can help that would be great.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 20:21:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1203041#M8591</guid>
      <dc:creator>Vidar</dc:creator>
      <dc:date>2022-08-15T20:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: List of layers TOC preserving nesting</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1203051#M8592</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;You can iterate through the layers in the TOC and determine if a layer is a Group layer or not using the "GroupLayer" type.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var map = MapView.Active.Map;
if (map == null)
  return;
//Get the group layers first
IReadOnlyList&amp;lt;GroupLayer&amp;gt; groupLayers = map.Layers.OfType&amp;lt;GroupLayer&amp;gt;().ToList();
//Iterate 
foreach (var groupLayer in groupLayers)
{
//Do something with groupLayer
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a snippet in the ProSDK wiki that iterates through the TOC. In this snippet, unchecked layers are removed from the TOC.&amp;nbsp;&amp;nbsp;&lt;A href="https://github.com/Esri/arcgis-pro-sdk/wiki/ProSnippets-MapAuthoring" target="_self"&gt;Removes all layers that are unchecked&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 20:52:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1203051#M8592</guid>
      <dc:creator>UmaHarano</dc:creator>
      <dc:date>2022-08-15T20:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: List of layers TOC preserving nesting</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1205240#M8632</link>
      <description>&lt;P&gt;Uma's suggestion works only for group layers, if you want a more general way to traverse the layer hierarchy you have to use the Layers collection of the Map class, as suggest by Uma.&amp;nbsp; But when you iterate through the layers you need to look at the real type of the layer object (in essence each class that derives from the Layer class) and if the derived type has a Layers collection you then iterate into that sub layer collection.&amp;nbsp; Below is a sample implementation, however, the implementation is incomplete because it only looks at AnnotationLayers and CompositeLayers as 2nd level entries in the TOC (there are more classes the derive from the Layer class and that have a Layers collection).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Wolf_0-1661218372195.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/49166i56F4BB498FDC3085/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Wolf_0-1661218372195.png" alt="Wolf_0-1661218372195.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void OnClick()
{
  try
  {
    var mva = MapView.Active;
    if (mva == null) return;
    var layers = mva.Map.Layers;
    StringBuilder sb = new();
    GetLayers(layers, sb, "");
    MessageBox.Show(sb.ToString());
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

private static bool GetLayers(ReadOnlyObservableCollection&amp;lt;Layer&amp;gt; layers, StringBuilder sb, string indent)
{
  foreach (var layer in layers)
  {
    var lyrName = layer.Name.Substring(0, Math.Min(40, layer.Name.Length));
    sb.AppendLine($@"{indent}{lyrName} {layer.GetType().Name}");
    if (layer is AnnotationLayer annoLyr)
    {
      GetLayers(annoLyr.Layers, sb, indent + "  ");
      continue;
    }
    if (layer is CompositeLayer compositeLyr)
    {
      GetLayers(compositeLyr.Layers, sb, indent + "  ");
      continue;
    }
  }
  return true;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 01:36:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/list-of-layers-toc-preserving-nesting/m-p/1205240#M8632</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-08-23T01:36:24Z</dc:date>
    </item>
  </channel>
</rss>

