<?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 C# removing layers &amp; empty grouplayers in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-removing-layers-empty-grouplayers/m-p/823718#M2939</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since I didn't see much out there (C#, SDK 2.1) on manipulating grouplayer, specifically removing them from the TOC if empty; here is an example.&amp;nbsp; Function "removes unchecked layers in the TOC" through a series of steps.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;private async Task X_TOC_lyrs()&lt;BR /&gt; {&lt;BR /&gt; await ActMap();//mapview.active.map checker&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Removes group-level layers that are unchecked, at once&lt;/P&gt;&lt;P&gt;IReadOnlyCollection&amp;lt;Layer&amp;gt; dlayers = MapView.Active.Map.Layers.Where(l =&amp;gt; l.IsVisible == false).ToList();&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayers(dlayers));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Removes subgroup-level layers that are unchecked, one-by-one&lt;/P&gt;&lt;P&gt;IEnumerable&amp;lt;Layer&amp;gt; dlayers2 = MapView.Active.Map.GetLayersAsFlattenedList().Where(l =&amp;gt; l.IsVisible == false).ToList();&lt;BR /&gt; foreach (var dlyr in dlayers2)&lt;BR /&gt; {&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayer(dlyr));&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Remove empty groups&lt;/P&gt;&lt;P&gt;IReadOnlyList&amp;lt;GroupLayer&amp;gt; glayers = MapView.Active.Map.Layers.OfType&amp;lt;GroupLayer&amp;gt;().ToList();&lt;BR /&gt; foreach (var lyr in glayers)&lt;BR /&gt; {&lt;BR /&gt; if(lyr.Layers.Count == 0)&lt;BR /&gt; {&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayer(lyr));&lt;BR /&gt; }&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 22 Jun 2018 13:53:57 GMT</pubDate>
    <dc:creator>MikeRatcliffe</dc:creator>
    <dc:date>2018-06-22T13:53:57Z</dc:date>
    <item>
      <title>C# removing layers &amp; empty grouplayers</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-removing-layers-empty-grouplayers/m-p/823718#M2939</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since I didn't see much out there (C#, SDK 2.1) on manipulating grouplayer, specifically removing them from the TOC if empty; here is an example.&amp;nbsp; Function "removes unchecked layers in the TOC" through a series of steps.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;private async Task X_TOC_lyrs()&lt;BR /&gt; {&lt;BR /&gt; await ActMap();//mapview.active.map checker&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Removes group-level layers that are unchecked, at once&lt;/P&gt;&lt;P&gt;IReadOnlyCollection&amp;lt;Layer&amp;gt; dlayers = MapView.Active.Map.Layers.Where(l =&amp;gt; l.IsVisible == false).ToList();&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayers(dlayers));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Removes subgroup-level layers that are unchecked, one-by-one&lt;/P&gt;&lt;P&gt;IEnumerable&amp;lt;Layer&amp;gt; dlayers2 = MapView.Active.Map.GetLayersAsFlattenedList().Where(l =&amp;gt; l.IsVisible == false).ToList();&lt;BR /&gt; foreach (var dlyr in dlayers2)&lt;BR /&gt; {&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayer(dlyr));&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Remove empty groups&lt;/P&gt;&lt;P&gt;IReadOnlyList&amp;lt;GroupLayer&amp;gt; glayers = MapView.Active.Map.Layers.OfType&amp;lt;GroupLayer&amp;gt;().ToList();&lt;BR /&gt; foreach (var lyr in glayers)&lt;BR /&gt; {&lt;BR /&gt; if(lyr.Layers.Count == 0)&lt;BR /&gt; {&lt;BR /&gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.RemoveLayer(lyr));&lt;BR /&gt; }&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Jun 2018 13:53:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/c-removing-layers-empty-grouplayers/m-p/823718#M2939</guid>
      <dc:creator>MikeRatcliffe</dc:creator>
      <dc:date>2018-06-22T13:53:57Z</dc:date>
    </item>
  </channel>
</rss>

