ArcGIS Pro 3.3 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / Layout Class / FindElements Method
The names of the elements to find. Cannot be null
Example

In This Topic
    FindElements Method (Layout)
    In This Topic
    Finds the elements recursively with the given names on the page layout.
    Syntax

    Parameters

    elementNames
    The names of the elements to find. Cannot be null

    Return Value

    A IList or an empty list if no elements are found
    Exceptions
    ExceptionDescription
    Value cannot be null: elementNames
    Remarks
    Find recurses nested groups to find the given elements.
    Example
    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");
    });
    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");
    });
    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");
    });
    Find layout elements
    //on the QueuedTask
    //Find elements by name
    var layoutElementsToFind = layout.FindElements(new List<string>() { "Point 1", "Line 3", "Text 1" });
    //Get the collection of elements from the page layout. Nesting within GroupElement is preserved.
    var elementCollection = layout.GetElements();
    //Get the collection of Element from the page layout as a flattened list. Nested groups within GroupElement are not preserved.
    var elements = layout.GetElementsAsFlattenedList();
    //Convert collection of the elements to a collection of GraphicElements.
    var graphicElements = elements.ToList().ConvertAll(x => (GraphicElement)x);
    //Find elements by type
    //Find all point graphics in the Layout
    var pointGraphics = graphicElements.Where(elem => elem.GetGraphic() is CIMPointGraphic);
    //Find all line graphics in the Graphics Layer
    var lineGraphics = graphicElements.Where(elem => elem.GetGraphic() is CIMLineGraphic);
    ////Find all polygon graphics in the Graphics Layer
    var polyGraphics = graphicElements.Where(elem => elem.GetGraphic() is CIMPolygonGraphic);
    ////Find all text graphics in the Graphics Layer
    var textGraphics = graphicElements.Where(elem => elem.GetGraphic() is CIMTextGraphic);
    ////Find all picture graphics in the Graphics Layer
    var pictureGraphic = graphicElements.Where(elem => elem.GetGraphic() is CIMPictureGraphic);
    UnSelect elements on the Layout
    //Unselect one element.
    var elementToUnSelect = layout.FindElements(new List<string>() { "MyPoint" }).FirstOrDefault();
    layout.UnSelectElement(elementToUnSelect);
    //Unselect multiple elements.
    var elementsToUnSelect = layout.FindElements(new List<string>() { "Point 1", "Line 3", "Text 1" });
    layout.UnSelectElements(elementsToUnSelect);
    UnSelect elements on the LayoutView
    LayoutView layoutView = LayoutView.Active;
    //Unselect one element.
    var elementToUnSelectInView = layout.FindElements(new List<string>() { "MyPoint" }).FirstOrDefault();
    layoutView.UnSelectElement(elementToUnSelect);
    //Unselect multiple elements.
    var elementsToUnSelectInView = layout.FindElements(new List<string>() { "Point 1", "Line 3", "Text 1" });
    layoutView.UnSelectElements(elementsToUnSelect);
    Zoom to elements
    LayoutView lytView = LayoutView.Active;
    //Zoom to an element
    var elementToZoomTo = layout.FindElements(new List<string>() { "MyPoint" }).FirstOrDefault();
    lytView.ZoomToElement(elementToZoomTo);
    //Zoom to  multiple elements.
    var elementsToZoomTo = layout.FindElements(new List<string>() { "Point 1", "Line 3", "Text 1" });
    lytView.ZoomToElements(elementsToZoomTo);
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also