How to access GroupLayers with ArcPro SDK

1527
7
10-05-2018 09:17 AM
ChrisBrannin
Occasional Contributor

Hello. I am trying to mimic something I have setup in Arc10.6 - ArcObjects to ArcPro 2.2 - ArcPro SDK and am having a hard time finding what I am looking for. Basically, I am trying to change the visibility of different group layers in the TOC. With ArcObjects I was able to use ICompositeLayer to easily get into the various group layers. Now... I'm sort of at a loss. I tried

MapView.Active.Map.Layers

AND

MapView.Active.Map.GetLayersAsFlattenedList().OfType<GroupLayer>()

Just not sure I'm using it correctly as it only gets me to the first level of grouplayers. Maybe I can just use the GroupLayer class?

The common structure of my group layers are - Group A > Group B > Group C > FeatureLayer.

Any tips or documentation for setting the visibility of a group layer would be greatly appreciated! 

0 Kudos
7 Replies
UmaHarano
Esri Regular Contributor

Hi Chris,

Just to confirm, does this line of code not return 3 Group layers when you nest them in the structure you mention (Group A > Group B > Group C)

MapView.Active.Map.GetLayersAsFlattenedList().OfType<GroupLayer>()

A test add-in with this logic worked for me, so wanted to confirm with you.

Thanks

Uma

0 Kudos
ChrisBrannin
Occasional Contributor

Thanks for the reply. It appears I was using it incorrectly! Once I used the ToList() I was able to get them all! Thank you.

0 Kudos
GiggsZhao
New Contributor

Hi Chris,

Maybe the following code can help you:

IReadOnlyList<GroupLayer> groupLayers = MapView.Active.Map.Layers.OfType<GroupLayer>().ToList();

GroupLayer groupLayer = null;
foreach (GroupLayer gLayer in groupLayers)
{
        if (gLayer.Name == string)
            groupLayer = gLayer;
}

I use it to got the grouplayer which I expected, so  maybe it can help you.

0 Kudos
ChrisBrannin
Occasional Contributor

Awesome, thank you. I landed on something similar and am now wondering, in which situations is it better to use one over the other?

MapView.Active.Map.GetLayersAsFlattenedList().OfType<GroupLayer>().ToList();

VS

IReadOnlyList<GroupLayer> groupLayers = MapView.Active.Map.Layers.OfType<GroupLayer>().ToList();

Thanks!

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

If you look at the output of Map.Layers and compare it to the Contents Pane in ArcGIS Pro you will notice that only the topmost layers of the table of contents, which shows the layers in a hierarchy, are returned.  When using Map.Layers you have to look at each item returned and then 'drill down' through all composite layers of each layer item.

If you use GetLayersAsFlattenedList you get all layers regardless of where in the table of content hierarchy the layer is located, so in essence the hierarchy is returned as a 'flattened list'.

0 Kudos
ChrisBrannin
Occasional Contributor

Great info, thank you.

0 Kudos
GiggsZhao
New Contributor

In ArcGIS Pro API Reference, you can see that GetLayersAsFlattenedList "returns a read only flat list of layers where nested groups are not preserved" and Layers “Gets a read-only collection of layers from the composite layer”.

0 Kudos