ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / ElementFactory Class / CreateGroupElement Method
The parent element container
The collection of elements to be grouped (optional)
An element name (optional)
Select after create flag (default is true) (optional)
Additional element properties for the group element (optional)
Example

In This Topic
    CreateGroupElement Method (ElementFactory)
    In This Topic
    Create a group element from the input element collection and element properties. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Function CreateGroupElement( _
       ByVal elementContainer As IElementContainer, _
       Optional ByVal elements As IEnumerable(Of Element), _
       Optional ByVal elementName As String, _
       Optional ByVal select As Boolean, _
       Optional ByVal groupElementInfo As ElementInfo _
    ) As GroupElement

    Parameters

    elementContainer
    The parent element container
    elements
    The collection of elements to be grouped (optional)
    elementName
    An element name (optional)
    select
    Select after create flag (default is true) (optional)
    groupElementInfo
    Additional element properties for the group element (optional)

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Remarks
    Only elements contained within the provided elementContainer will be added to the group element. If elements is null or an empty collection then an empty GroupElement will be created.
    Example
    Create_Empty_Group_Root
    //Create an empty group element at the root level of the contents pane.
    
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement emptyGroupAtRoot = LayoutElementFactory.Instance.CreateGroupElement(layout);
      //         emptyGroupAtRoot.SetName("Empty group at root");
      GroupElement emptyGroupAtRoot = ElementFactory.Instance.CreateGroupElement(layout, null, "Empty group at root");
    });
    Create_Empty_Group_Group
    //Create an empty group element at the root level of another group element.
    
    //Find an existing group element
    GroupElement existingGroupAtRoot = layout.FindElement("Empty group at root") as GroupElement;
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement emptyGroupInGroupAtRoot = LayoutElementFactory.Instance.CreateGroupElement(existingGroupAtRoot);
      //         emptyGroupInGroupAtRoot.SetName("Empty group in group at root");
      GroupElement emptyGroupInGroupAtRoot = ElementFactory.Instance.CreateGroupElement(
                                              existingGroupAtRoot, null, "Empty group in group at root");
    
    });
    Create_Group_With_Single_Element_Root
    //Create a group with a single element at the root level of the contents pane.
    
    //Find an existing element
    Element titleElm = layout.FindElement("Title") as Element;
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement groupWithSingleElementAtRoot = LayoutElementFactory.Instance.CreateGroupElement(layout, titleElm);
      //         groupWithSingleElementAtRoot.SetName("Group with single element at root"); 
      GroupElement groupWithSingleElementAtRoot = 
               ElementFactory.Instance.CreateGroupElement(layout, new List<Element>() { titleElm }, "Group with single element at root");
    });
    Create_Group_With_List_Elements_Root
    //Create a group with a list of elements at the root level of the contents pane.
    
    //Find an existing elements
    Element scaleBar = layout.FindElement("Scale Bar") as Element;
    Element northArrow = layout.FindElement("North Arrow") as Element;
    Element legend = layout.FindElement("Legend") as Element;
    
    //Build a list and add the elements
    List<Element> elmList = new List<Element>
    {
      scaleBar,
      northArrow,
      legend
    };
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement groupWithListOfElementsAtRoot = LayoutElementFactory.Instance.CreateGroupElement(layout, elmList);
      //         groupWithListOfElementsAtRoot.SetName("Group with list of elements at root");
      GroupElement groupWithListOfElementsAtRoot = 
        ElementFactory.Instance.CreateGroupElement(layout, elmList, "Group with list of elements at root");
    });
    Create_Group_With_List_Element_Names_Root
    //Create a group using a list of element names at the root level of the contents pane.
    
    //Build list of element names
    var elmNameList = new[] { "Table Frame", "Chart Frame" };
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //GroupElement groupWithListOfElementNamesAtRoot = LayoutElementFactory.Instance.CreateGroupElement(layout, elmNameList);
      //groupWithListOfElementNamesAtRoot.SetName("Group with list of element names at root");
    
      //At 3.x use FindElements to retrieve the respective elements first
      var elems = layout.FindElements(elmNameList);
      GroupElement groupWithListOfElementNamesAtRoot =
        ElementFactory.Instance.CreateGroupElement(layout, elems, "Group with list of element names at root");
    });
    Creating empty group elements
    //Create an empty group element at the root level of the contents pane
    
    //Create on worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement grp1 =
      //             LayoutElementFactory.Instance.CreateGroupElement(layout);
      //         grp1.SetName("Group"); 
    
      //container is IElementContainer - GroupLayer or Layout
      GroupElement grp1 = ElementFactory.Instance.CreateGroupElement(
                            container, null, "Group");
    });
    
    // *** or ***
    
    //Create a group element inside another group element
    
    //Find an existing group element
    //container is IElementContainer - GroupLayer or Layout
    GroupElement existingGroup = container.FindElement("Group") as GroupElement;
    
    //Create on worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement grp2 =
      //      LayoutElementFactory.Instance.CreateGroupElement(existingGroup);
      //         grp2.SetName("Group in Group");
      GroupElement grp2 = ElementFactory.Instance.CreateGroupElement(
        existingGroup, null, "Group in Group");
    });
    Create a group element with elements
    //Create a group with a list of elements at the root level of the contents pane.
    
    //Find an existing elements
    //container is IElementContainer - GroupLayer or Layout
    var elem1 = container.FindElement("Polygon 1");
    var elem2 = container.FindElement("Bezier Text");
    var elem3 = container.FindElement("Cloud Shape 2");
    
    //Construct a list and add the elements
    var elmList = new List<Element>
    {
      elem1,
      elem2,
      elem3
    };
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement groupWithListOfElementsAtRoot =
      //             LayoutElementFactory.Instance.CreateGroupElement(layout, elmList);
      //groupWithListOfElementsAtRoot.SetName("Group with list of elements at root");
      //
      GroupElement groupWithListOfElementsAtRoot =
              ElementFactory.Instance.CreateGroupElement(
                       container, elmList, "Group with list of elements at root");
    });
    
    // *** or ***
    
    //Create a group using a list of element names at the root level of the contents pane.
    
    //List of element names
    var elmNameList = new[] { "Para Text1", "Line 3" };
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //At 2.x - GroupElement groupWithListOfElementNamesAtRoot =
      //   LayoutElementFactory.Instance.CreateGroupElement(layout, elmNameList);
      //         groupWithListOfElementNamesAtRoot.SetName(
      //                  "Group with list of element names at root");
    
      //At 3.x, use the names to find the relevant elements first
      //container is IElementContainer - GroupLayer or Layout
      var elems = container.FindElements(elmNameList);
      GroupElement groupWithListOfElementNamesAtRoot =
          ElementFactory.Instance.CreateGroupElement(
               container, elems, "Group with list of element names at root");
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also