Overview

You will learn: how to build data editing workflows with your add-in, updating feature attribute data using the Inspector class.

With 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, you can build Pro add-ins and configurations that provide your users with custom functionality specific to their organization or industry.

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

This lab will walk you through creation of an add-in which updates the attributes of selected features for a feature class you specify in your project, based on an attribute name and a value that you provide in custom edit boxes.

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

  1. You will now add three controls to your add-in which will be used on the ribbon, starting with two edit boxes. Right click the project and choose Add > New Item and then select the Visual C# empty class definition from the list of item templates. When prompted, name the new class file AttributeNameToUseEditBox.cs and press Add to close the dialog box. Perform this same operation again to add another class definition file and this time name it ModValueToSetEditBox.cs.

  2. Right click the project and choose Add > New Item and then from the ArcGIS Pro Add-ins group select ArcGIS Pro Button from the list of item templates. When prompted, name the new class file UpdateValuesButton.cs and press Add to close the dialog box. The ArcGIS Pro Button template provides basic contents of the class file, as well as corresponding code in the Config.daml file for the Pro UI.

Update code for the controls

  1. Open the AttributeNameToUseEditBox.cs file and replace the contents of the class so it looks like the following:

    class AttributeNameToUseEditBox : ArcGIS.Desktop.Framework.Contracts.EditBox
    {
        public AttributeNameToUseEditBox()
        {
            Module1.Current.AttributeNameToUseEditBox1 = this;
            Text = "";
        }
    }
    
  2. In the same way, open the ModValueToSetEditBox.cs file and replace the contents of the class so it looks like the following:

    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:

            Module1.Current.UpdateValues();
    

Update code in 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 code:

      <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="Images\GenericButtonBlue16.png" largeImage="Images\GenericButtonBlue32.png">
        </button>
      </controls>
    
  2. Open the Module1.cs file and paste the following code below the Overrides region:

        #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(true);
                    inspector.Load(featLayer, featSelectionOIDs);
                    if (inspector.HasAttributes && inspector.Count(a => a.FieldName == attributename) > 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. Your code is complete. Build your project and debug any issues. Start ArcGIS Pro, and when the start page appears, select the previously-installed Pro sample project: C:\Data\FeatureTest\FeatureTest.aprx.

  2. When the project has loaded, go to the Add-In tab and find your new add-in’s group labeled Update Attributes with Inspector. This confirms your add-in was properly built and added. Next, select one of the feature layers from the Contents pane, such as TestPoints, and right-click to open its attribute table view. Select a few records that you would like to try updating on the Description attribute. Make sure you have the correct layer selected in the TOC, and a few features selected and showing in the attribute table.

  3. Enter values into your add-in’s edit boxes. For the attribute name edit box, type in Description as this is the name of the attribute you will update. For the value to set edit box, as the TestPoints’ Description attribute is a string field, you can type in any value you like. Next, press the Update Values button to run the edit operation and update the selected records. The operation should tell you if a step is missing, and will also confirm if the edit operation completed.

  4. If the operation completes successfully, check to see that your value was applied for the selected records.

Congratulations, you're done!

Check out and compare with our completed solution project.

Challenge

Review the online documentation

Read more information about editing data with the Pro SDK in the ProConcepts Editing document, and try some of the code snippets.

Try a community sample

Explore the Editing community samples on GitHub.

Review other patterns for updating data

The ArcGIS Pro Geodatabase API has different patterns for querying and updating data, read about these in the ProConcepts Geodatabase document.