<?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 Re: Nested group layers with ArcPy in ArcGIS Pro in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1312989#M68272</link>
    <description>&lt;P&gt;Hi Johannes,&lt;/P&gt;&lt;P&gt;wow, this is really much more complicated than I would have hoped for.&lt;/P&gt;&lt;P&gt;Thank your very, very much for looking into this and all the effort you invested. I will try to incooperate your solution into my scripts once I am finding the time for it (right now, there is also a lot of other work to do, unfortunately ). In any case, I will come back here and provide some feedback on how it worked out for me.&lt;/P&gt;&lt;P&gt;Again, thank you so much for now! &lt;span class="lia-unicode-emoji" title=":grinning_cat_face:"&gt;😺&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jul 2023 09:14:12 GMT</pubDate>
    <dc:creator>lbd_bayern</dc:creator>
    <dc:date>2023-07-28T09:14:12Z</dc:date>
    <item>
      <title>Nested group layers with ArcPy in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1311973#M68248</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;I am trying to create a multi-level layer hierarchy of group layers with ArcPy.&lt;/P&gt;&lt;P&gt;Consider the following example:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="example.png" style="width: 271px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76342i3B30AFE39E844FC8/image-dimensions/271x202?v=v2" width="271" height="202" role="button" title="example.png" alt="example.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;At the top of the hierarchy, let's say I have a group layer named &lt;STRONG&gt;level_1_group_layer&lt;/STRONG&gt;. This group layer has two group layers as children, &lt;STRONG&gt;level_2_group_layer_a&lt;/STRONG&gt; and &lt;STRONG&gt;level_2_group_layer_b&lt;/STRONG&gt;. Each of these two group layers has two "normal" layers as children (&lt;STRONG&gt;level_3_layer_a&lt;/STRONG&gt; and &lt;STRONG&gt;level_3_layer_b&lt;/STRONG&gt; as children of&amp;nbsp;&lt;STRONG&gt;level_2_group_layer_a&lt;/STRONG&gt;,&amp;nbsp;&lt;STRONG&gt;level_3_layer_c&lt;/STRONG&gt; and &lt;STRONG&gt;level_3_layer_d&lt;/STRONG&gt; as children of&amp;nbsp;&lt;STRONG&gt;level_2_group_layer_b&lt;/STRONG&gt;).&lt;/P&gt;&lt;P&gt;Is there a way to achieve this nested hierarchy of group layers via ArcPy?&lt;/P&gt;&lt;P&gt;I am able to place "normal" layers (e.g.&amp;nbsp;&lt;FONT face="courier new,courier"&gt;level_3_layer_a&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;level_3_layer_b&lt;/FONT&gt;) below a group layer (e.g. &lt;FONT face="courier new,courier"&gt;level_2_group_layer_a&lt;/FONT&gt;). For this, I am using the method &lt;FONT face="courier new,courier"&gt;addLayerToGroup(group, layer)&lt;/FONT&gt;&amp;nbsp;of the &lt;FONT face="courier new,courier"&gt;activeMap&lt;/FONT&gt; object as described here:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-questions/is-it-possible-to-create-a-new-group-layer-with/td-p/1068607" target="_blank" rel="noopener"&gt;Solved: Is it possible to create a new group layer with ar... - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;However, so far, I was not successful in adding a group layer (e.g. &lt;FONT face="courier new,courier"&gt;level_2_group_layer_a&lt;/FONT&gt;) as a child of another group layer (e.g. &lt;FONT face="courier new,courier"&gt;level_1_group_layer&lt;/FONT&gt;).&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Trying to use the method &lt;FONT face="courier new,courier"&gt;addLayerToGroup(level_1_group_layer, level_2_group_layer_a)&lt;/FONT&gt; results in an exception:&amp;nbsp;&lt;FONT face="courier new,courier" color="#FF0000"&gt;Traceback (most recent call last):&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 114, in &amp;lt;module&amp;gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 53, in remove_layer_from_map__and__place_layer_below_group_layer&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;return fn(*args, **kw)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2593, in addLayerToGroup&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;ValueError: &amp;lt;MappingLayerObject object at 0x000001701373CED0&amp;gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;For &lt;FONT face="courier new,courier"&gt;activeMap&lt;/FONT&gt;, I found the method &lt;FONT face="courier new,courier"&gt;moveLayer(&lt;SPAN&gt;reference_layer, move_layer, {insert_position}&lt;/SPAN&gt;)&lt;/FONT&gt;&amp;nbsp;(&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm" target="_blank" rel="noopener"&gt;Map—ArcGIS Pro | Documentation&lt;/A&gt;) which from the description seems to be exactly what I need (with e.g.&amp;nbsp;&lt;FONT face="courier new,courier"&gt;level_1_group_layer&lt;/FONT&gt; as &lt;FONT face="courier new,courier"&gt;reference_layer&lt;/FONT&gt; and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;level_2_group_layer_a&lt;/FONT&gt; as &lt;FONT face="courier new,courier"&gt;move_layer&lt;/FONT&gt;). However, for the parameter &lt;FONT face="courier new,courier"&gt;insert_position&lt;/FONT&gt;, only the values &lt;FONT face="courier new,courier"&gt;"AFTER"&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;"BEFORE"&lt;/FONT&gt; are available. For &lt;FONT face="courier new,courier"&gt;"AFTER"&lt;/FONT&gt;, it is stated that&amp;nbsp; it "i&lt;SPAN&gt;nserts the new layer after or below the reference layer". When I now call &lt;FONT face="courier new,courier"&gt;active_map.move_layer(level_1_group_layer, level_2_group_layer_a, "AFTER")&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;level_2_group_layer_a&amp;nbsp;&lt;/FONT&gt;is inserted after &lt;FONT face="courier new,courier"&gt;level_1_group_layer&lt;/FONT&gt;, i.e. on the same hierarchy level. Is there any way to a ensure it is inserted below , i.e. that &lt;FONT face="courier new,courier"&gt;level_2_group_layer_a&amp;nbsp;&lt;/FONT&gt;will be one level below &lt;FONT face="courier new,courier"&gt;level_1_group_layer&amp;nbsp;&lt;/FONT&gt;in the layer hierarchy?&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;Am I using the methods incorrectly? Or are there alternative ways to achieve the nested hierarchy of group layers? Any help is very much appreciated!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 13:01:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1311973#M68248</guid>
      <dc:creator>lbd_bayern</dc:creator>
      <dc:date>2023-07-26T13:01:32Z</dc:date>
    </item>
    <item>
      <title>Re: Nested group layers with ArcPy in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1312879#M68271</link>
      <description>&lt;P&gt;So, this was an adventure...&lt;/P&gt;&lt;P&gt;You can do it, but it's complicated and there are several pitfalls along the way.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;You can't add an empty group layer file to an existing group layer (as you found out)&lt;/LI&gt;&lt;LI&gt;You can only add layers to a group layer, so you have to convert your input feature classes first&lt;/LI&gt;&lt;LI&gt;You could do it with MakeFeatureLayer, but there was some problem ( I forgot what it was exactly)&lt;/LI&gt;&lt;LI&gt;So the best way I found is to use map.addDataFromPath, add the resulting layer to a group with map.addLayerToGroup and then delete the original layer with map.removeLayer&lt;/LI&gt;&lt;LI&gt;You need to traverse your layer list recursively to be able to add group layers to group layers&lt;/LI&gt;&lt;LI&gt;Bonus: You can't use the same lyrx when you are in recursive mode. When you add an empty group lyrx to the map and then try to add the same lyrx to it, it will raise a RuntimeError and complain about missing credentials.&lt;/LI&gt;&lt;LI&gt;So you actually need to create new empty layer files as you go, so you need to find a way to do that (fortunately, it is quite easy)&lt;/LI&gt;&lt;LI&gt;And then you realize that it always adds layers to the top of the toc, so you need to find a way to combat that. addDataToPath doesn't have a position argument, so you would have to do it with map.moveLayer, but I did not want to go down that rabbit hole, so I just used reversed() in 2 places to traverse the layer structure from end to start. This works for feature classes with the same geometry type, but arcpy changes position according to geometry type (points above lines above polygons), so it might not work for different geometry types.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With all that said, here is the very ugly result:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path
from uuid import uuid4


def create_group_layer_file(folder, name):
    """Creates an empty group layer file.

    folder: str or pathlib.Path, the folder where the file will be saved
    name: str, the name of the layer (without extension)

    Returns a str of the layer file's path

    """
    uRI = f"CIMPATH={uuid4()}.json"  # unique id for the layer
    cim = {
        "type" : "CIMLayerDocument", # this is a layer document
        "layers" : [                 # these are the included layers
            uRI
            ],
        "layerDefinitions" : [       # and these are the layers' definitions
            {
              "type" : "CIMGroupLayer",
              "name" : name,
              "uRI" : uRI
            }
          ]
        }
    # create the file
    lyrx = Path(folder) / f"{name}.lyrx"
    with lyrx.open("w") as f:
        f.write(str(cim))
    # return the path as str
    return str(lyrx)


def add_layer_recursive(target_map, layer, folder, group=None):
    """Adds a layer to a map. If the specified layer is a group layer, it adds
    all its children recursively.

    target_map: arcpy.mp.Map, the map to which the layer will be added
    layer: str or dict, the layer that will be added
        str: full path to a feature class, this will add a feature layer
        dict: dictionary with the keys "name" and "layers", this will add
            a group layer with the specified name and then it will add all
            specified layers to this group
    folder: str or pathlib.Path, a folder where temporary layer files will be created
        you need write access to that folder!
    group: arcpy.mp.Layer, the group layer to which teh layer will be added
        only important for recursion, call the function with group=None

    """
    # layer is path to feature class
    if isinstance(layer, (str, Path)):
        print(f"Adding {layer} to the map")
        lyr = target_map.addDataFromPath(str(layer))
        if group is not None:
            print(f"\tMoving {layer} to group layer {group.name}")
            target_map.addLayerToGroup(group, lyr)
            target_map.removeLayer(lyr)
    # layer is group layer definition dictionary
    elif isinstance(layer, dict):
        print(f"Adding group layer {layer['name']} to the map")
        new_group_lyrx = create_group_layer_file(folder, layer["name"]) # create lyrx
        new_group = target_map.addDataFromPath(new_group_lyrx)
        new_group.name = layer["name"]
        Path(new_group_lyrx).unlink() # delete lyrx
        # call this function recursively
        for sub_layer in reversed(layer["layers"]):
            add_layer_recursive(target_map, sub_layer, temp_dir, new_group)
        # and move the subgroup
        if group is not None:
            print(f"\tMoving group layer {new_group.name} to group layer {group.name}")
            target_map.addLayerToGroup(group, new_group)
            target_map.removeLayer(new_group)




# the map to which the layers will be added
target_map = arcpy.mp.ArcGISProject("current").activeMap
# a folder where you have write access
temp_dir = r"G:\ArcGIS\data\temp"
# the layer structure
    # feature layers as path to their source (str or pathlib.Path)
    # group layers as dict with the keys "name" and "layers"
gdb = Path(arcpy.env.workspace)
layers = [
    gdb/"FC0",
    {"name": "GroupA", "layers":[
        gdb/"FC1",
        gdb/"FC2",
        ]},
    gdb/"FC3",
    {"name": "GroupB", "layers":[
        {"name": "GroupBA", "layers": [
            gdb/"FC4",
            gdb/"FC5",
            ]},
        {"name": "GroupBB", "layers": [
            gdb/"FC6",
            {"name": "GroupBBA", "layers": [
                gdb/"FC7",
                gdb/"FC8",
                ]},
            ]},
        ]},
    gdb/"FC9",
    ]


# create the temp dir
Path(temp_dir).mkdir(exist_ok=True)
# add the layers
for layer in reversed(layers):
    add_layer_recursive(target_map, layer, temp_dir, None)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because the function works recursively, you can do as many levels as you like. Maybe ArcGIS has some limit, but the function doesn't.&lt;/P&gt;&lt;P&gt;For example, the layer structure in lines 86 and onwards nests 3 group layers:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1690497746337.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76626iF123CA557048B69A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1690497746337.png" alt="JohannesLindner_0-1690497746337.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 22:43:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1312879#M68271</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-07-27T22:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Nested group layers with ArcPy in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1312989#M68272</link>
      <description>&lt;P&gt;Hi Johannes,&lt;/P&gt;&lt;P&gt;wow, this is really much more complicated than I would have hoped for.&lt;/P&gt;&lt;P&gt;Thank your very, very much for looking into this and all the effort you invested. I will try to incooperate your solution into my scripts once I am finding the time for it (right now, there is also a lot of other work to do, unfortunately ). In any case, I will come back here and provide some feedback on how it worked out for me.&lt;/P&gt;&lt;P&gt;Again, thank you so much for now! &lt;span class="lia-unicode-emoji" title=":grinning_cat_face:"&gt;😺&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 09:14:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1312989#M68272</guid>
      <dc:creator>lbd_bayern</dc:creator>
      <dc:date>2023-07-28T09:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Nested group layers with ArcPy in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1324233#M68505</link>
      <description>&lt;P&gt;So I finally found time testing this.&lt;/P&gt;&lt;P&gt;I am a bit confused because the following happened:&lt;/P&gt;&lt;P&gt;Before, I was able to move Feature Layers below a Group Layer, essentially using this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def move_feature_layer_below_group_layer(feature_layer, group_layer, target_map):
        target_map.removeLayer(feature_layer)
        target_map.addLayerToGroup(group_layer, feature_layer)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When trying to move Group Layers below a Group Layer, the code failed (as described in my original post).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Now - for whatever reason - it is exactly the other way round&lt;/STRONG&gt;: Using the two methods above, I can move Group Layers below a Group Layer, but trying to move a Feature Layer below a Group Layer fails.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the first case, when moving Group Layers below a Group Layer, it also works as intended if these moved Group Layers are parents of Feature Layers. So this is great for my use case. See the following example, displaying the initial state on the left and the result after running my script on the right (I manually arranged the initial state on the left in ArcGIS Pro):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GroupLayers_below_GroupLayer.png" style="width: 685px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/79543i6470093087884318/image-size/large?v=v2&amp;amp;px=999" role="button" title="GroupLayers_below_GroupLayer.png" alt="GroupLayers_below_GroupLayer.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For the second case (moving Feature Layers below a Group Layer), my once working script now produces the following error:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Traceback (most recent call last):&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 201, in &amp;lt;module&amp;gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 111, in hf_remove_layer_from_map__and__readd_layer_below_group_layer&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return fn(*args, **kw)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2593, in addLayerToGroup&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;ValueError: &amp;lt;MappingLayerObject object at 0x00000162667A60C0&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Have there been any changes to the API?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am very confused because according to the install info&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;arcpy.GetInstallInfo()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;, my installation should still be the same as when I made my original post:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;{'LicenseLevel': 'Basic', 'InstallDir': 'c:\\program files\\arcgis\\pro\\', 'ProductName': 'ArcGISPro', 'Version': '3.1.2', 'SourceDir': '1', 'InstallType': 'N/A', 'BuildNumber': '41833', &lt;STRONG&gt;'InstallDate': '30.06.2023'&lt;/STRONG&gt;, 'InstallTime': '12:21:38'}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Any ideas what is causing the problems, or suggestions how I can now move Feature Layers below a Group Layer?&lt;/P&gt;&lt;P&gt;For moving Feature Layers, I also tried the trick with the lyrx file from Johannes. Here my hard-coded, simple version (no need for recursion here):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def move_feature_layer_below_group_layer_using_lyrx(feature_layer, group_layer, target_map):
    new_feature_lyrx = hf__create_feature_layer_file(r"C:\MyPrograms\temp\tmp_files", feature_layer.name)
    new_feature = target_map.addDataFromPath(new_feature_lyrx)
    new_feature.name = feature_layer.name
    target_map.addLayerToGroup(group_layer, new_feature)
    target_map.removeLayer(new_feature)
    Path(new_feature_lyrx).unlink()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the following version of the subroutine&amp;nbsp;&lt;FONT face="courier new,courier"&gt;hf__create_feature_layer_file&lt;/FONT&gt;, I am getting the same error as above (red text):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def hf__create_feature_layer_file(folder, name):
    uRI = f"CIMPATH={uuid4()}.json"  # unique id for the layer
    cim = {
    "type" : "CIMLayerDocument", # this is a layer document
    "layers" : [                 # these are the included layers
        uRI
        ],
    "layerDefinitions" : [       # and these are the layers' definitions
        {
          "type" : "CIMFeatureLayer",
          "name" : name,
          "uRI" : uRI
        }
      ]
    }
    # create the file
    lyrx = Path(folder) / f"{name}.lyrx"
    with lyrx.open("w") as f:
        f.write(str(cim))
    # return the path as str
    return str(lyrx)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Traceback (most recent call last):&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 236, in &amp;lt;module&amp;gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 171, in hf_remove_layer_from_map__and__readd_layer_below_group_layer2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return fn(*args, **kw)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2593, in addLayerToGroup&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;ValueError: &amp;lt;MappingLayerObject object at 0x000001629A97AE40&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Using a slightly adapted version of &lt;FONT face="courier new,courier"&gt;hf__create_feature_layer_file&lt;/FONT&gt;, I am getting a different error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def hf__create_feature_layer_file(folder, name, feature_layer):
    uRI = f"CIMPATH={uuid4()}.json"  # unique id for the layer
    cim = {
    "type" : "CIMLayerDocument", # this is a layer document
    "layers" : [                 # these are the included layers
        feature_layer
        ],
    "layerDefinitions" : [       # and these are the layers' definitions
        {
          "type" : "CIMFeatureLayer",
          "name" : name,
          "uRI" : uRI
        }
      ]
    }
    # create the file
    lyrx = Path(folder) / f"{name}.lyrx"
    with lyrx.open("w") as f:
        f.write(str(cim))
    # return the path as str
    return str(lyrx)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Traceback (most recent call last):&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 236, in &amp;lt;module&amp;gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "&amp;lt;string&amp;gt;", line 169, in hf_remove_layer_from_map__and__readd_layer_below_group_layer2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return fn(*args, **kw)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2764, in addDataFromPath&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path, web_service_type, custom_parameters,), True)))&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;RuntimeError: Daten konnten nicht hinzugefügt werden. Mögliches Problem bei den Anmeldeinformationen.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;(The German sentence at the end translates to "Data could not be added. Possible problem with the credentials.")&lt;/P&gt;&lt;P&gt;This error indicates a problem with the writing permissions. I am not sure why this occurs, though, as my user definitely has write permissions for the specified folder.&lt;/P&gt;&lt;P&gt;I am running my scripts in the ArcGIS Pro Python window, if this would make any difference for the described problems.&lt;/P&gt;&lt;P&gt;Sorry for the wall of text.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 07:35:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-group-layers-with-arcpy-in-arcgis-pro/m-p/1324233#M68505</guid>
      <dc:creator>lbd_bayern</dc:creator>
      <dc:date>2023-08-31T07:35:34Z</dc:date>
    </item>
  </channel>
</rss>

