Obtain list of toolbox items in toolbox at runtime?

380
2
Jump to solution
01-27-2023 07:22 PM
JR99
by
New Contributor II

Is it possible to open an existing toolbox (*.tbx) and read the tools into a collection?

Something along the lines of:

var toolboxPath = "C:\Data\ToolboxExample.tbx";
var toolbox = Toolbox.Open(toolboxPath);
ToolboxItemsCollection toolboxItems = toolBox.GetToolsList();

I'm wanting to write a WPF usercontrol that displays a list of all the geoprocessing tools in the toolbox, rather than needing to access them through the Catalog. I won't know at compile time which tools are in the toolbox so adding them as buttons in the DAML is not an option.

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Code for toolbox content below:

var gpItems = CoreModule.CurrentProject.Items.OfType<GeoprocessingProjectItem>();

      // go through all the available toolboxes
      foreach (var gpItem in gpItems)
      {
          var itemsInsideToolBox = gpItem.GetItems();

          // then for each toolbox list the tools inside
          foreach (var toolItem in itemsInsideToolBox)
          {
              string newTool = String.Join(";", new string[] { toolItem.Path, toolItem.Name });
              // do something with the newTool
              // for example, add to a list to track or use them later
          }
      }

If your toolbox is not added to project try to create it using ItemFactory:

var item = ItemFactory.Instance.Create(toolboxPath);

And add it to project.

More info here:

GeoprocessingProjectItem Class—ArcGIS Pro

Create Method (ItemFactory)—ArcGIS Pro

 

 

View solution in original post

2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Code for toolbox content below:

var gpItems = CoreModule.CurrentProject.Items.OfType<GeoprocessingProjectItem>();

      // go through all the available toolboxes
      foreach (var gpItem in gpItems)
      {
          var itemsInsideToolBox = gpItem.GetItems();

          // then for each toolbox list the tools inside
          foreach (var toolItem in itemsInsideToolBox)
          {
              string newTool = String.Join(";", new string[] { toolItem.Path, toolItem.Name });
              // do something with the newTool
              // for example, add to a list to track or use them later
          }
      }

If your toolbox is not added to project try to create it using ItemFactory:

var item = ItemFactory.Instance.Create(toolboxPath);

And add it to project.

More info here:

GeoprocessingProjectItem Class—ArcGIS Pro

Create Method (ItemFactory)—ArcGIS Pro

 

 

JR99
by
New Contributor II

Thanks GintautasKmieliauskas! That's exactly what I was looking for.

 
 
0 Kudos