Get attributes of currently selected feature

883
1
07-04-2021 05:31 PM
woodsynzl
New Contributor

I'm currently working on an add-in with .NET.

I am wanting to get an attribute from the selected feature (if more than one selected, just the first item).

Eg: user selects something in editor > user presses 'do something from selected' > add in 

 I can't get my head around how I can achieve this. So far I'm thinking that:

 

MapView.Archive.GetSelectedLayers();

 

 Is my closest bet - however I've probably been looking at it too long!

Thanks in advance!

0 Kudos
1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

To get the current 'selection' from the map you want to use this method:

 

var selection = MapView.Active.Map.GetSelection();

 

GetSelection returns a 'dictionary' defined as: Dictionary<MapMember, List<long>>

In essence for each MapMember (MapMember is the common base class for StandaloneTables, Layers, FeatureLayers, etc) you get a list of object ids for all selected rows or features for that particular MapMember.  

Using the returned 'selection' you have to first chose one 'MapMember' or 'FeatureLayer' for which you want to get the attribute, The following snippet finds a MapMember with a selected set by name:

 

var selectedMapMember = selection.Keys.Where(mapMember => mapMember.Name.Equals(layerName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() as FeatureLayer;
if (selectedMapMember == null) return;

 

Next you can use the selectedMapMember to search for the selected rows.  

Putting it all together (in a button's OnClick method) your workflow would look like this:

 

protected override void OnClick()
{
  // set the layer name to choose
  var layerName = "TestPoint";
  // select the attribute column
  var columnName = "Description";

  // initiate the following code to execute on the MCT
  QueuedTask.Run(() =>
  {
    try
    {
      // get the current selection for the active map
      var selection = MapView.Active.Map.GetSelection();
      // selection is of type: Dictionary<MapMember, List<long>>
      // first we find the FeatureLayer we are interested in:
      var selectedMapMember = selection.Keys.Where(mapMember => mapMember.Name.Equals(layerName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() as FeatureLayer;
      if (selectedMapMember == null)
      {
        MessageBox.Show($@"No selection for: {layerName}");
        return;
      }
      // selectedMapMember is the layer we are interested in
      // now we get the row or features
      using (var cursor = selectedMapMember.Search(new QueryFilter { ObjectIDs = selection[selectedMapMember] }))
      {
        while (cursor.MoveNext())
        {
          using (var feature = cursor.Current as Feature)
          {
            // process each attribute
            MessageBox.Show($@"oid: {feature.GetObjectID()} {columnName}: {feature[columnName]}");
          }
        }
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show($@"Exception: {ex}");
    }
  });
}