ArcGIS Pro SDK Add-In C#: How to Browse to feature class in SDE geodatabase

1062
2
Jump to solution
01-20-2022 06:53 AM
JadedEarth
Occasional Contributor

Hi,

This was so easy to do in ArcObjects, but now in Pro, I'm lost in trying to call basic dialogs.

How do I open a dialog that allows user to browse to features contained in an SDE database connection file?

Appreciate any help.

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This is how I open a dialog to browse datasets. The full list of BrowseProjectFilters can be found here

First, I create the command that I use in a button on my form (SelectionPane)

  public ICommand OpenFCBrowseDialog { get; set; }

  protected SelectionPaneViewModel()
    {
      BindingOperations.EnableCollectionSynchronization(_featureLayers, _lockFeaturelayers);
      ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChanged);
      ActivePaneChangedEvent.Subscribe(OnActivePaneChanged);
      MapSelectionChangedEvent.Subscribe(OnMapSelectionChanged);
      LayersAddedEvent.Subscribe(OnLayersAdded);
      LayersRemovedEvent.Subscribe(onLayersRemoved);
      OnActivePaneChanged(null);

      OpenFCBrowseDialog = new RelayCommand((param) => OpenBrowseDialog(param), () => true);

    }

 

The button can open a polygon feature class in some cases, or any type of feature class in other cases

    private async void OpenBrowseDialog(object param)
    {
      string button = param.ToString();
      string FeatureType;

      switch (button)
      {
        case "InputFCButton":
          FeatureType = "Polygon";
          break;
        default:
          FeatureType = "Any";
          break;
      }
      Item item = Utilities.BrowsetoOpenDataset(FeatureType);
      if (item == null) return;
      await QueuedTask.Run(() =>
      {
        using (var fc = GDBItemHelper.GetDatasetFromItem(item) as FeatureClass)
        {
          lock (_featureLayers) ;
          //await QueuedTask.Run(() => LayerFactory.Instance.CreateFeatureLayer(fc, MapView.Active.Map, 0));
          LayerFactory.Instance.CreateFeatureLayer(fc, MapView.Active.Map, 0);
        }
      });

    }

 

In my Utilities class, I have this function to open the different feature classes

    public static Item BrowsetoOpenDataset(string DSType)
    {

      BrowseProjectFilter bf;

      switch (DSType)
      {
        case "Polygon":
          bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_polygon");
          break;
        //case "Feature Class":
        //  bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_all");
        //  break;
        default:
          bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_all");
          break;
      }

      OpenItemDialog op = new OpenItemDialog
      {
        Title = "Open " + DSType,
        MultiSelect = false,
        BrowseFilter = bf
      };
      bool? ok = op.ShowDialog();
      return ok.HasValue && op.Items.Count() > 0 ? op.Items.First() : null;
    }

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

This is how I open a dialog to browse datasets. The full list of BrowseProjectFilters can be found here

First, I create the command that I use in a button on my form (SelectionPane)

  public ICommand OpenFCBrowseDialog { get; set; }

  protected SelectionPaneViewModel()
    {
      BindingOperations.EnableCollectionSynchronization(_featureLayers, _lockFeaturelayers);
      ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChanged);
      ActivePaneChangedEvent.Subscribe(OnActivePaneChanged);
      MapSelectionChangedEvent.Subscribe(OnMapSelectionChanged);
      LayersAddedEvent.Subscribe(OnLayersAdded);
      LayersRemovedEvent.Subscribe(onLayersRemoved);
      OnActivePaneChanged(null);

      OpenFCBrowseDialog = new RelayCommand((param) => OpenBrowseDialog(param), () => true);

    }

 

The button can open a polygon feature class in some cases, or any type of feature class in other cases

    private async void OpenBrowseDialog(object param)
    {
      string button = param.ToString();
      string FeatureType;

      switch (button)
      {
        case "InputFCButton":
          FeatureType = "Polygon";
          break;
        default:
          FeatureType = "Any";
          break;
      }
      Item item = Utilities.BrowsetoOpenDataset(FeatureType);
      if (item == null) return;
      await QueuedTask.Run(() =>
      {
        using (var fc = GDBItemHelper.GetDatasetFromItem(item) as FeatureClass)
        {
          lock (_featureLayers) ;
          //await QueuedTask.Run(() => LayerFactory.Instance.CreateFeatureLayer(fc, MapView.Active.Map, 0));
          LayerFactory.Instance.CreateFeatureLayer(fc, MapView.Active.Map, 0);
        }
      });

    }

 

In my Utilities class, I have this function to open the different feature classes

    public static Item BrowsetoOpenDataset(string DSType)
    {

      BrowseProjectFilter bf;

      switch (DSType)
      {
        case "Polygon":
          bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_polygon");
          break;
        //case "Feature Class":
        //  bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_all");
        //  break;
        default:
          bf = new BrowseProjectFilter("esri_browseDialogFilters_featureClasses_all");
          break;
      }

      OpenItemDialog op = new OpenItemDialog
      {
        Title = "Open " + DSType,
        MultiSelect = false,
        BrowseFilter = bf
      };
      bool? ok = op.ShowDialog();
      return ok.HasValue && op.Items.Count() > 0 ? op.Items.First() : null;
    }
0 Kudos
JadedEarth
Occasional Contributor

Hi,

Thank you for this.  Very helpful and I was able to adopt it to my needs.  I needed to get the name of the polygon located inside an SDE geodatabase.  So when the user clicks the button, the OpenItemDialog allows the user to navigate to the SDE geodatabase, open it, and select the polygon needed; then fills the Textbox with the name of the polygon.

Following is my solution on button_click event:

 

private void BtnInFtrBrowse_Click(object sender, RoutedEventArgs e)
        {
            var bpf = new ArcGIS.Desktop.Core.BrowseProjectFilter("esri_browseDialogFilters_featureClasses_polygon");
            bpf.Name = "SDE Spatial Table";
            
            string DSType = "Polygon";

            OpenItemDialog op = new OpenItemDialog
            {
                Title = "Open " + DSType,                
                InitialLocation = TboxPath.Text,
                MultiSelect = false,
                BrowseFilter = bpf
            };

            bool? ok = op.ShowDialog();

            if (ok == true)
            {
                foreach(ArcGIS.Desktop.Core.Item p in op.Items)
                {
                    TboxInFeature.Text = p.Title;
                }                
            }
        }

 

 

Thanks so much for the link to the new ESRI dialog namespaces.