Edit attribute data

This tutorial demonstrates how to build data editing workflows with your add-in, updating feature attribute data using the Inspector class.

With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, developers can build Pro add-ins and solution configurations that provide users with custom functionality specific to their organization or industry.

The ArcGIS Pro SDK provides an extensive Editing API that supports different data editing patterns based on your workflow. One of these patterns includes use of the Inspector class for getting and setting feature attribute values.

In this tutorial, you will create an add-in that updates the attributes of selected features from a feature class you specify within your project that is based on an attribute name and a value that you provide in custom edit boxes.

Prerequisites

Steps

Create a new ArcGIS Pro Add-In Visual Studio Project

  1. Start Visual Studio.

  2. Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project EditWithInspector.

Add Ribbon controls to your add-in

You will now add three controls to your add-in to be used on the ribbon.

  1. Right-click the project and choose Add > New Item and then select the Visual C# empty class definition from the list of item templates.

  2. When prompted, name the new class file AttributeNameToUseEditBox.cs and then click Add to close the dialog box.

  3. Repeat this action again to add another class definition file. This time name the new class file ModValueToSetEditBox.cs.

  4. Right-click the project a third time and click Add > New Item and then from the ArcGIS Pro Add-ins group, select ArcGIS Pro button from the list of item templates.

  5. When prompted, name the new class file UpdateValuesButton.cs and then click Add to close the dialog box. The ArcGIS Pro Button template provides the basic contents of the class file, as well as corresponding code in the Config.daml file for the ArcGIS Pro UI.

Update code for the controls

  1. Open the AttributeNameToUseEditBox.cs file and replace the contents of the class with the following:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    class AttributeNameToUseEditBox : ArcGIS.Desktop.Framework.Contracts.EditBox
    {
        public AttributeNameToUseEditBox()
        {
            Module1.Current.AttributeNameToUseEditBox1 = this;
            Text = "";
        }
    }
  2. Open the ModValueToSetEditBox.cs file and replace the contents of the class with the following:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    class ModValueToSetEditBox : ArcGIS.Desktop.Framework.Contracts.EditBox
    {
        public ModValueToSetEditBox()
        {
            Module1.Current.ModValueToSetEditBox1 = this;
            Text = "";
        }
    }
  3. Update the UpdateValuesButton.cs file and add the following line to the OnClick method:

    Use dark colors for code blocksCopy
    1
            Module1.Current.UpdateValues();

Update code in the Config.daml and Module1.cs files

  1. Open the Config.daml file and replace the contents of the groups and controls sections with the following:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
      <groups>
        <group id="EditWithInspector_Group1" caption="Update Attributes with Inspector" appearsOnAddInTab="true">
          <editBox refID="EditWithInspector_AttributeNameToUseEditBox" size="middle" />
          <editBox refID="EditWithInspector_ModValueToSetEditBox" size="middle" />
          <button refID="EditWithInspector_UpdateValuesButton" size="large" />
        </group>
      </groups>
    
      <controls>
        <editBox id="EditWithInspector_ModValueToSetEditBox" caption="Enter value to set: " className="ModValueToSetEditBox" sizeString="AAAAAAAAAAAAAAAAA"></editBox>
        <editBox id="EditWithInspector_AttributeNameToUseEditBox" caption="Enter attribute name to update:" className="AttributeNameToUseEditBox" sizeString="AAAAAAAAAAAAAAAAA"></editBox>
        <button id="EditWithInspector_UpdateValuesButton" caption="Update Values" className="UpdateValuesButton" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
        </button>
      </controls>
  2. Open the Module1.cs file and paste the following below the Overrides region:

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
        #region Business Logic
    
        // Create instances of the edit boxes for use in the UpdateValues method
        public AttributeNameToUseEditBox AttributeNameToUseEditBox1 { get; set; }
        public ModValueToSetEditBox ModValueToSetEditBox1 { get; set; }
    
        public void UpdateValues()
        {
            //  This sample is intended for use with a featureclass with a default text field named "Description".
            //  You can replace "Description" with any field name that works for your dataset
    
            // Check for an active mapview
            if (MapView.Active == null)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No MapView currently active. Exiting...", "Info");
                return;
            }
    
            QueuedTask.Run(() =>
            {
    
                // Get the layer selected in the Contents pane, and prompt if there is none:
                if (MapView.Active.GetSelectedLayers().Count == 0)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No feature layer selected in Contents pane. Exiting...", "Info");
                    return;
                }
                // Check to see if there is a selected feature layer
                var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
                if (featLayer == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected. Exiting...", "Info");
                    return;
                }
                // Get the selected records, and check/exit if there are none:
                var featSelectionOIDs = featLayer.GetSelection().GetObjectIDs();
                if (featSelectionOIDs.Count == 0)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No records selected for layer, " + featLayer.Name + ". Exiting...", "Info");
                    return;
                }
                // Ensure there are values in the two edit boxes
                else if (ModValueToSetEditBox1.Text == "" || AttributeNameToUseEditBox1.Text == "")
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Editbox for attribute name or value to set is empty. Type in value. Exiting...", "Value Needed");
                    return;
                }
    
                // Get the name of the attribute to update, and the value to set:
                string attributename = AttributeNameToUseEditBox1.Text;
                attributename = attributename.ToUpper();
                string setvalue = ModValueToSetEditBox1.Text;
    
                // Display all the parameters for the update:
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Here are your update parameters:  " +
                    "\r\n Layer: " + featLayer.Name +
                    "\r\n Attribute name: " + attributename +
                    "\r\n Number of records: " + Convert.ToString(featSelectionOIDs.Count) +
                    "\r\n Value to update: " + Convert.ToString(setvalue), "Update");
    
                try
                {
                    // Now ready to do the actual editing:
                    // 1. Create a new edit operation and a new inspector for working with the attributes
                    // 2. Check to see if a valid field name was chosen for the feature layer
                    // 3. If so, apply the edit
    
                    //
                    var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();
                    inspector.Load(featLayer, featSelectionOIDs);
                    if (inspector.HasAttributes && inspector.Count(a => a.FieldName.ToUpper() == attributename.ToUpper()) > 0)
                    {
                        inspector[attributename] = setvalue;
                        var editOp = new EditOperation();
                        editOp.Name = "Edit " + featLayer.Name + ", " + Convert.ToString(featSelectionOIDs.Count) + " records.";
                        editOp.Modify(inspector);
                        editOp.ExecuteAsync();
    
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Update operation completed.", "Editing with Inspector");
                    }
                    else
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("The Attribute provided is not valid. " +
                            "\r\n Ensure your attribute name is correct.", "Invalid attribute");
                        // return;
                    }
                }
                catch (Exception exc)
                {
                    // Catch any exception found and display a message box.
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to perform update: " + exc.Message);
                    return;
                }
            });
        }
        #endregion

Build and test your code

  1. Once your code is complete, build your project and debug any issues. Start ArcGIS Pro, and when the start page appears, select the previously-installed ArcGIS Pro sample project C:\Data\FeatureTest\FeatureTest.aprx.

  2. When the project has loaded, from the Add-In tab, find your new add-in’s group labeled Update Attributes with Inspector. This confirms that your add-in was properly built and added. Next, select a feature layer from the Contents pane, such as TestPoints, and right-click to open its attribute table view. Select a few records and update their Description attribute. Ensure you have the correct layer selected in the TOC, and a few features selected and shown in the attribute table.

  3. Enter values into your add-in’s edit boxes:

    • For attribute name, enter Description.

    • Since the TestPoints’ Description attribute is a string, enter any value.

  4. Click Update Values to run the edit operation and update the selected records. The operation should report if a step is missing, and confirm if the edit operation completed successfully.

  5. If the operation completes successfully, confirm that your value was applied to the selected records.

What's Next?

Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.