ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / LayoutProjectItem Class / GetLayout Method
Example

In This Topic
    GetLayout Method
    In This Topic
    Loads and returns the Layout associated with the LayoutPrjectItem. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public Layout GetLayout()
    Public Function GetLayout() As Layout

    Return Value

    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    CIMGraphic
    //This example references a graphic element on a layout and sets its Transparency property (which is not available in the managed API)
    //by accessing the element's CIMGraphic.
    
    //Added references
    using ArcGIS.Core.CIM;                             //CIM
    using ArcGIS.Desktop.Core;                         //Project
    using ArcGIS.Desktop.Layouts;                      //Layout class
    using ArcGIS.Desktop.Framework.Threading.Tasks;    //QueuedTask
    
    public class GraphicElementExample1
    {
      public static Task<bool> UpdateElementTransparencyAsync(string LayoutName, string ElementName, int TransValue)
      {
        //Reference a layoutitem in a project by name
        LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(LayoutName));
        if (layoutItem == null)
          return Task.FromResult(false);
    
        return QueuedTask.Run<bool>(() =>
        {
          //Reference and load the layout associated with the layout item
          Layout lyt = layoutItem.GetLayout();
    
          //Reference a element by name
          GraphicElement graElm = lyt.FindElement(ElementName) as GraphicElement;
          if (graElm == null)
            return false;
    
          //Modify the Transparency property that exists only in the CIMGraphic class.
          CIMGraphic CIMGra = graElm.GetGraphic() as CIMGraphic;
          CIMGra.Transparency = TransValue;             //e.g., TransValue = 50
          graElm.SetGraphic(CIMGra);
    
          return true;
        });
      }
    }
    LayoutProjectItem_GetLayout
    //Reference the layout associated with a layout project item.
    
    LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("Layout Name"));
    Layout layout = await QueuedTask.Run(() => layoutItem.GetLayout());  //Perform on the worker thread
    Layout_ShowProperties
    //Open the layout properties dialog.
    
    //Get the layout associated with a layout project item
    LayoutProjectItem lytItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("Layout Name"));
    Layout lyt = await QueuedTask.Run(() => lytItem.GetLayout());  //Worker thread
    
    //Open the properties dialog
    lyt.ShowProperties();  //GUI thread
    LayoutAdded_ProjectItemsChangedEvent_Add
    //Report the event args when a layout is added.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutAddedEvent.Subscribe((args) =>
    //{
    //  System.Windows.MessageBox.Show("LayoutAddedEvent:" +
    //    Environment.NewLine +
    //    "   arg Layout: " + args.Layout.Name);
    //});
    
    //Use ProjectItemsChangedEvent at 3.x
    ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Subscribe((args) => {
      //Layout added. Layout removed would be NotifyCollectionChangedAction.Remove
      if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add &&
          args.ProjectItem is LayoutProjectItem layoutProjectItem)
      {
        var layout_name = layoutProjectItem.Name; 
        var layout = layoutProjectItem.GetLayout();
        System.Diagnostics.Debug.WriteLine($"Layout Added: {layout_name}");
      }
    });
    
    LayoutRemoved_ProjectItemsChangedEvent_Remove
    //Report the event args when a layout is removed.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutRemovedEvent.Subscribe((args) =>
    //{
    //  System.Windows.MessageBox.Show("LayoutViewEvent:" +
    //  Environment.NewLine +
    //  "   arg Layout: " + args.Layout.Name);
    //});
    
    //Use ProjectItemsChangedEvent at 3.x
    ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Subscribe((args) => {
      //Layout added. Layout removed would be NotifyCollectionChangedAction.Remove
      if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove &&
          args.ProjectItem is LayoutProjectItem layoutProjectItem)
      {
        var layout_name = layoutProjectItem.Name;
        System.Diagnostics.Debug.WriteLine($"Layout Removed: {layout_name}");
      }
    });
    
    LayoutRemoved_ProjectItemsChangedEvent_Removing
    //Report the event args when a layout is about to be removed.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutRemovingEvent.Subscribe((args) =>
    //{
    //  if (args.LayoutPath == "CIMPATH=layout.xml")
    //  {
    //    args.Cancel = true;
    //  }
    //  return Task.FromResult(0);
    //});
    
    //At 3.x use ProjectItemRemovingEvent
    ArcGIS.Desktop.Core.Events.ProjectItemRemovingEvent.Subscribe((args) =>
    {
      var layoutItems = args.ProjectItems.ToList().OfType<LayoutProjectItem>() ?? new List<LayoutProjectItem>();
      var layoutName = "DontDeleteThisOne";
      foreach(var layoutItem in layoutItems)
      {
        if (layoutItem.Name == layoutName)
        {
          args.Cancel = true;//Cancel the remove
          break;
        }
      }
      return Task.FromResult(0);
    });
    
    LayoutView_FindAndCloseLayoutPanes
    //Find and close all layout panes associated with a specific layout.
    
    LayoutProjectItem findLytItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("Layout"));
    Layout findLyt = await QueuedTask.Run(() => findLytItem.GetLayout());  //Perform on the worker thread
    
    var panes = ProApp.Panes.FindLayoutPanes(findLyt);
    foreach (Pane pane in panes)
    {
      ProApp.Panes.CloseLayoutPanes(findLyt.URI);  //Close the pane
    }
    LayoutView_LayoutFrameWorkExtender
    //This sample checks to see if a layout project item already has an open application pane.  
    //If it does, it checks if it is the active layout view, if not, it creates, activates and opens a new pane.
    
    //Reference a layoutitem in a project by name
    LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("Layout View"));
    
    //Get the layout associated with the layoutitem
    Layout lyt = await QueuedTask.Run(() => layoutItem.GetLayout());
          
    //Iterate through each pane in the application and check to see if the layout is already open and if so, activate it
    foreach (var pane in ProApp.Panes)
    {
      var layoutPane = pane as ILayoutPane;
      if (layoutPane == null)  //if not a layout pane, continue to the next pane
        continue;
      if (layoutPane.LayoutView.Layout == lyt)  //if the layout pane does match the layout, activate it.
      {
        (layoutPane as Pane).Activate();
        layoutPane.Caption = "This is a test";
        System.Windows.MessageBox.Show(layoutPane.Caption);
        return;
      }
    }
    //Otherwise, create, open, and activate the layout if not already open
    ILayoutPane newPane = await ProApp.Panes.CreateLayoutPaneAsync(lyt);
    
    //Zoom to the full extent of the layout
    await QueuedTask.Run(() => newPane.LayoutView.ZoomTo100Percent());
    Open a layout project item in a new view
    //Open a layout project item in a new view.
    //A layout project item may exist but it may not be open in a view. 
    
    //Reference a layout project item by name
    LayoutProjectItem someLytItem = Project.Current.GetItems<LayoutProjectItem>()
                              .FirstOrDefault(item => item.Name.Equals("MyLayout"));
    
    //Get the layout associated with the layout project item
    Layout layout = await QueuedTask.Run(() => someLytItem.GetLayout());  //Worker thread
    
    //Create the new pane - call on UI
    ILayoutPane iNewLayoutPane = await ProApp.Panes.CreateLayoutPaneAsync(layout); //GUI thread
    Change the layout page size
    //Change the layout page size.
    
    //Reference the layout project item
    LayoutProjectItem lytItem = Project.Current.GetItems<LayoutProjectItem>()
                             .FirstOrDefault(item => item.Name.Equals("MyLayout"));
    if (layoutItem != null)
    {
      await QueuedTask.Run(() =>
      {
        //Get the layout
        Layout lyt = lytItem.GetLayout();
        if (lyt != null)
        {
          //Change properties
          CIMPage page = lyt.GetPage();
          page.Width = 8.5;
          page.Height = 11;
    
          //Apply the changes to the layout
          lyt.SetPage(page);
        }
      });
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also